1. Save the file as textwriter.php in the PHPSCRIPTS folder.

    Here's what the relevant lines in this script do:
  • $myfile = fopen ($filename, 'w') or die("Can not open file");

    The form in textwriter.html has a hidden text field named filename. Here the script requests the value assigned to it in the form—textthought.txt, then assigns that value to the variable $myfile.

    The 'w' means the text file textthough.txt ($myfile) is opened for writing.

    If the file does not exist, a new one will be created. If the file already exists, it will overwrite the existing file.

    If there is a problem with the file opening, the process will stop (or die) and display the message "Can not open file".
  • $outputFile = "$comments\n\n";

    This line adds two "new line" characters (\n\n) to the end of the comments submitted through the form on textwriter.html.

    Any new data added to the file textthought.txt starts on a new paragraph. This new data is then assigned to the variable $outputFile.

  • fputs($myfile, $outputFile);

    fputs gets the comments from the form ($outputFile), and puts them in the file associated with the $myfile variable—textthought.txt.

  • fclose($myfile);

    The fclose command tells the Web server that you’re done using the $myfile variable.

    TIP: When using the fopen() command, there are six ways a file can be opened:

    r Read only.
    r+ Read and write.
    w Write only.
    w+ Read and over-write.
    a Append.
    a+ Read and append.