Php Writing To A File

windows

Guest
Hi all, <br /><br />Need some input on where to start with this. I've got a nightly CRON job set up that refers to a PHP file that goes through my database and deletes any entries that are over 10 days old. What I'd like it to do is write to a file how many entries were deleted. I'd like it to append the information to the end of the file so I'd have a running record of what was deleted each night. <br /><br />Eventually I'd like it to put today's date in the file, the titles of the entries over 10 days old, and then a confirmation line that those were deleted. For instance, I'd like the file to say something like: <br /><br />2006-07-02: <br />Entry Title1<br />Entry Title2<br />Entry Title3<br />Total of 3 entries deleted<br /><br />2006-07-03: <br />Entry Title4<br />Entry Title5<br />Total of 2 entries deleted<br /><br />2006-07-04: <br />Entry Title6<br />Entry Title7<br />Entry Title8<br />Entry Title9<br />Total of 4 entries deleted<br /><br />I can pull today's date and the entry titles that will be deleted out of the database, and then run the mysql delete command to get rid of them. I mean, I can set up the script to output this information in this format - what I'm not getting is how to send that information to a file. I'm guessing from my research so far I'll need to use fopen() to do it, but the examples I'm finding are WAY too complicated for my pea brain to process. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /> <br /><br />Anyone got a sample SIMPLE fopen() example?<!--content-->
For what you are wanting to do it use a combination of fopen, fwrite, and fclose.<br /><br />Here is a very basic example:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->// This example asumes you have whatever you want to be writen to the file in a variable named "$data"<br />// Also, your target file needs be CHMOD 0777 (or you will get an error back)<br /><br />// Open Connection to Target File<br />$handle = fopen("[Filename]", "a");  // the "a" is the mode, a = "open for writing, place start position at END of file<br /><br />// Write Data to file<br />fwrite ($handle, $data);  <br /><br />// Close Connection<br />fclose($handle);<!--c2--></div><!--ec2--><br /><br />Referances for each function:<br />fopen:<a href="http://us2.php.net/manual/en/function.fopen.php" target="_blank">http://us2.php.net/manual/en/function.fopen.php</a><br />fwrite:<a href="http://us2.php.net/manual/en/function.fwrite.php" target="_blank">http://us2.php.net/manual/en/function.fwrite.php</a><br />fclose:<a href="http://us2.php.net/manual/en/function.fclose.php" target="_blank">http://us2.php.net/manual/en/function.fclose.php</a><br /><br /><br /><br /><br />I hope this helps<!--content-->
Perfect, thanks! The submitted code bits in the online manual are all waaaaaayyyy more complicated than I needed to get me started, and surprisingly the O'Reilly Programming PHP book doesn't even address fopen except from a security standpoint. <br /><br />Thanks again for the example! <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/biggrin.gif" style="vertical-align:middle" emoid=":D" border="0" alt="biggrin.gif" /><!--content-->
 
Back
Top