Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Read log file with PHP?



4:49 am on May 3, 2007 (gmt 0)

5+ Year Member

It turns out that opendir and a number of other cool PHP features are disabled by my host. I've had to deal with similar issues before and have worked around them (that's part of the fun.)
What I am trying to do is create a PHP file which will delete specific files from a directory that are more than 24 hours old.
My question is this:
Is this possible without readdir and opendir? Can I use fopen and fread as alternatives in my solution?
The key problem that I see is that I can read the log file but where do I begin to parse the log file into file names and dates?
Typical entries in my log file would look like this:

2007/05/02 2:00:31 :
File http://www.example.com/myFile.zip
Sent to someone@example.com

2007/05/02 2:08:31 :
File http://www.example.com/myPic.jpg
Sent to someone2@anotherexample.com

I guess I would have to somehow define the format of each entry? If so how would I do this.
Is this idea even realistic with PHP?
If anyone has any ideas or ideas about different approaches please let me know.

postscript: okay, that's more than one question.


5:09 am on May 3, 2007 (gmt 0)

5+ Year Member

Oh, I forgot to mention, that I need to extract the time and file name so that I can identify which files to delete. This might f already been obvious but no harm in clarifying.


11:48 am on May 3, 2007 (gmt 0)

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

can you use the directory class?


3:48 pm on May 3, 2007 (gmt 0)

5+ Year Member

Could you not just check the file creating time using fstat or similar function and then just use unlink to delete the file?



9:13 pm on May 6, 2007 (gmt 0)

10+ Year Member

Does your host allows you to use the FTP functions [php.net] of PHP? You then may use these to FTP to your server and delete the files using ftp_delete [php.net].
FTP also provides functions for reading, creating and deleting directories.


1:22 am on May 7, 2007 (gmt 0)

5+ Year Member

I looked into all of the suggestions submitted here and each has its own merit, however, it seems that my biggest hurdle is that I won't necessarily know the names of these files to target them. I only know where they will be. Also I can't delete all of the files at once only those that are more than 24 hours old. That's why I thought in lieu of the functions not available to me because of shared server constraints (noted at the beginning of this post) I thought I could create a script to parse the information I needed from my detailed log files to identify specific files that met the criteria for deletion. I didn't glean that I could do this with the examples suggested. If I am wrong could someone elaborate on how I would apply the presented options?


8:05 am on May 7, 2007 (gmt 0)

10+ Year Member

If I understand your posts correctly you have a log file in the format described in your first post.
Is this format fixed or can you change the script that writes this logfile so it writes one entry per line in a format that can be parsed easily by another script like e.g.:

2007/05/02 2:00:31 http://www.example.com/myFile.zip someone@example.com
2007/05/02 2:08:31 http://www.example.com/myPic.jpg someone2@anotherexample.com

Is this possible? If not we have to make a more complex parser to parse the logfile in the format you described.

Featured Threads

Hot Threads This Week

Hot Threads This Month