Contact Page messages added to a daily txt file.

Hi

Thanks for the clues to previous problems.
Managed to get moving on the updates now.

http://www.probowluk.co.uk

How can I get the Contact Form fields appended to a daily txt file ?

cheers

  Bob Sharp

get todays date
http://www.php.net/manual/en/function.date.php

open file with name todays date + .txt, and set the pointer to the end of the file as we want to add data to the end
http://www.php.net/manual/en/function.fopen.php

'a+' 	Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it. 

lock file (so that multiple simultaneous requests will not mess up your file)
http://www.php.net/manual/en/function.flock.php

add your form data to an array (set fields to the posted data or null if not set, this way you will get the same setup for each entry even if some are missing fields)

save the array as csv to a file
http://www.php.net/manual/en/function.fputcsv.php

flush data buffer
http://www.php.net/manual/en/function.fflush.php

unlock the file so the next request can take over
http://www.php.net/manual/en/function.flock.php

close file handler
http://www.php.net/manual/en/function.fclose.php

[hr]

then you can just read the file, use fgetcsv and get an array of all the inputted data that day.

Here’s what I did …

[code]if ( ($cError == “”) OR ($cError == NULL) )
{
$FolderName = “formdata/”;
$dateYmd = date(‘Ymd’);
$FileName = “_Contact.txt”;

$file_name = $FolderName . $dateYmd . $FileName; 
$open_file = fopen($file_name, "a+");

$file_record = $_POST['cFname'] . "¬" . $_POST['cSname'] . "¬";
$file_record .= $_POST['cMail'] . "¬" . "...";		
$file_record .= $_POST['cTxt'] . "¬"; 			
$file_record .= "\r\n";				 
$file_record .= "++++++++++++++++++++++++++++++++++++++" . "\r\n" . "\r\n";		

$file_name = $FolderName . $dateYmd . $FileName; 
$open_file = fopen($file_name, "a+");
if (flock($file_name, LOCK_EX)) {  // acquire an exclusive lock
   ftruncate($file_name, 0);      // truncate file
   fwrite($file_name, $file_record);
   fflush($file_name);            // flush output before releasing the lock
   flock($file_name, LOCK_UN);    // release the lock
} else {
   echo "Couldn't get the lock!";
}
		
fclose($open_file); 
// $cError = ""; // 
// header("Location: Contact_Done.php");  // 

}

?>
[/code]

Here are some errors that showed up …
[font=arial]
Warning: fopen(formdata/20140203_Contact.txt) [function.fopen.php]: failed to open stream: Permission denied in D:\probowluk.co.uk\wwwroot\Contact_proc.php on line 77

Warning: fopen(formdata/20140203_Contact.txt) [function.fopen.php]: failed to open stream: Permission denied in D:\probowluk.co.uk\wwwroot\Contact_proc.php on line 86

Warning: flock() expects parameter 1 to be resource, string given in D:\probowluk.co.uk\wwwroot\Contact_proc.php on line 87[/font]

[size=8pt]Couldn’t get the lock![/size]

[font=arial]Warning: fclose(): supplied argument is not a valid stream resource in D:\probowluk.co.uk\wwwroot\Contact_proc.php on line 96[/font]

You need to give the web server user write permissions to that folder.

Two days on that problem and now I discover I have to contact the Server HelpDesk and get them to change the permissions !

HelpDesk ticket issued and status is “awaiting”

Sponsor our Newsletter | Privacy Policy | Terms of Service