PHP lets you store data in text files on a server. This may for example be used to store data entered in an HTML form. You may then read the data from the file and display it in a web page or use it for other purposes.
Below is a basic PHP example that stores data from a PHP variable in a file on a server. The file is located in the same direcory as the PHP page that writes the data file.
$fileName = "mydata.txt"; $file=fopen($fileName,"w"); // Open the file with "w" access to replace the existing data fputs($file, $comment); // Write the new data to the file fclose($file); // Close the file
The example above will replace any previous contents in the file with the new data. If you want to keep the old contents of the file and append the data from the PHP variable(s) you must open the file in the "a" append mode, $file=fopen($fileName,"a");, as shown below:
$fileName = "mydatabase.txt"; $file=fopen($fileName,"a"); // Open the file with "a" access to append data fputs($file, $comment."\n"); // Append the new data + a linefeed to the end of the file fclose($file); // Close the file
See:
Counter Example
Form To File Example
This PHP code example is copyright © 2002-2003 Optima System. All rights reserved worldwide. Unauthorized distribution prohibited. SpinPHP™ User License.