Jump to content

Php Writing To A File


Recommended Posts

Hi all,


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.


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:



Entry Title1

Entry Title2

Entry Title3

Total of 3 entries deleted



Entry Title4

Entry Title5

Total of 2 entries deleted



Entry Title6

Entry Title7

Entry Title8

Entry Title9

Total of 4 entries deleted


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. :D


Anyone got a sample SIMPLE fopen() example?

Link to comment
Share on other sites

For what you are wanting to do it use a combination of fopen, fwrite, and fclose.


Here is a very basic example:

>// This example asumes you have whatever you want to be writen to the file in a variable named "$data"
// Also, your target file needs be CHMOD 0777 (or you will get an error back)

// Open Connection to Target File
$handle = fopen("[Filename]", "a");  // the "a" is the mode, a = "open for writing, place start position at END of file

// Write Data to file
fwrite ($handle, $data);  

// Close Connection


Referances for each function:








I hope this helps

Edited by Dman8568
Link to comment
Share on other sites

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.


Thanks again for the example! :D

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...