Clearing a scripts "memory"

Discussion in 'PHP' started by Dakafall, Jun 23, 2010.

  1. #1
    I am just starting to get into PHP and I have written my first script. What this script does is it takes 2 entries from a user (first and last name) and creates a file on the server with that first and last name. Also, the script checks whether or not a file with that name already exists.

    My problem is, the script always says "This name already has a file" no matter what now.

    Any help would be appreciated!

    
    <!DOCTYPE html PUBLIC
    	"-//W3C//DTD XHTML 1.0 Strict//EN"
    	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
    <title> PHP TEST </title>
    </head>
    <body>
    <div>
    <form method="post" action="<?php print $_SERVER['PHP_SELF']?>">
    <p>
    Type your first name here: <input type="text" name="fname" />
    <br/>
    Type your last name here: <input type="text" name="lname" />
    </p>
    <input type="submit" action="submit" />
    </form>
    <?php
    ini_set('display_errors', 0);
    $filename = "$_POST[fname].$_POST[lname]";
    $fo = fopen( $filename, "w");
    fwrite( $fo, "First Name : \n");
    fwrite( $fo, "$_POST[fname] \n" );
    fwrite( $fo, "Last Name : \n");
    fwrite( $fo, "$_POST[lname]" );
    fclose( $fo );
    
    if (file_exists($filename)) {
    	print "This name already has a file";
    } else {
    	print "Your file is now complete :D";
    }
    ?>
    </div>
    </body>
    </html>
    PHP:
     
    Dakafall, Jun 23, 2010 IP
  2. Fervid

    Fervid Well-Known Member

    Messages:
    161
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You need to check to see if the form data has been posted. When you load the script, the filename is "." which technically always exists.
     
    Fervid, Jun 23, 2010 IP