PDA

View Full Version : Script to write text file... just some help tweaking



sirbrent
04-08-2008, 04:19 PM
Hello :)

I have written a little script that accepts form data and writes it to a file.

<?php
$date = date("d/m/Y");
$server_path = "output/"; //Path to log directory
$log_entries = $date. "\n" .$_POST['name']. "\n" .$_POST['email']. "\n" .$_POST['street']. "\n" .$_POST['city']. "\n" .$_POST['state']. "\n" .$_POST['zipcode']. "\n" .$_POST['comment']. "\n\n";

$fp = fopen($server_path . "form.txt", "w+") or die("ERROR! Cannot create 'form.txt' file. Perhaps the output directory has incorrect permissions?");

if ($fp!= false)

{

fwrite($fp, $log_entries);
fclose($fp);

echo "<SCRIPT LANGUAGE=\"javascript\"><!--\n";
echo " alert (\"Your information has been added! Thank you!\");\n";
echo "// --></SCRIPT>\n";
header( "Location: ../thankyou.html" );
}
?>

it works fine, but i do want to change the way it works and i would really appreciate some help.

As it is written now it writes the data to the text file, but overwrites any data that already exists. I would like it to append data to the file, not overwrite the existing data.

Many thanks for taking the time to read my post!
-Brent

sirbrent
04-08-2008, 04:34 PM
I have answered my own question.
Thanks anyway!

sjlplat
04-08-2008, 06:38 PM
For future reference:

Appending to a flat file requires the fwrite() +a flag. ;)