Save form data to text file

Here is a very simple script that will save any form data to a text file on the server.

This is very useful if there is a potential for email or database issues with any form submissions


/////////////////////////// WRITE TO FILE ///////////////////////////
$sDate = date("mdy");
//open the file and choose the mode
$fh = fopen("/home/foldername/submissions/".$sDate.".txt", "a");
fwrite($fh, $sData);
//close the file
/////////////////////////// END WRITE TO FILE ///////////////////////////

The code is very simple.  $sDate defines the date format.  I use this so the file name will be the current date.

The $fh variable gives the fopen command along with the file name and location and the fopen mode to use.  In this case we have used ‘a’.  ‘a’ Opens the file, if it doesnt exist, it will attempt to create it, then it will put the cursor at the end of the file.  This will allow for the newest entry to be at the bottom of the text file.  If you want to change this, you can look at the values here:

Fwrite will actually do the writing.    It takes the values of $fh and also the actual data, which I have defined as $sData.

Make sure to close the file write with fclose!

You may also like...