Hi! I need a simple php script (a few lines of code probably) which will delete all the files in a directory that are older than x hours. for ex: to delete all the files in a directory that are older than 12 hours In exchange I'll give you a featured link in my webmastertag.com PR3 directory Thanks!
Are you sure you need PHP? If you are using a linux server the find command might be your friend: find /dir -cmin +720 -exec rm {} \; Code (markup):
In PHP it could be solved like this: <?php $dir = "/tmp/"; # directory to check $dh = opendir($dir); while(false !== ($file = readdir($dh))) { $file = $dir.$file; if (filetype($file) != "file") continue; # Only files if (filectime($file) < (time() - (12 * 3600))) { # 12 hours old unlink($file); # Delete file } } closedir($dh); ?> Code (markup): But if your server is running linux I would always prefer the solution with "find". PHP is oversized for just deleting some files.
There could be many reasons why he wants to use PHP... CRON job, he doesn't have shell access, etc. Just one thing to note on chengfu's code... filectime($file) determines the file's CREATION time... if you want to look at the file's last modified time, use filemtime($file).
thanks you! just one more thing: how can I exclude a file to not be deleted? let's say test.html - I want this to remain on the server I intend to run this script via a cron or everytime another php file is accessed (via php include) chengfu and sketch - please each one add your site to webmastertag.com and let me know so I can approve it (you'll get a featured link)
Off the top of my head, if you only want to test for the one file (i.e. test.html), the last IF statement should be if (($file != "test.html") AND filectime($file) < (time() - (12 * 3600))). If you want to test for a list of files, create an array with those files, then the last IF statement should be if (!in_array($donotdeletethese) AND filectime($file) < (time() - (12 * 3600))) {. I didn't mean to hijack chengfu's code :$ ... but I've added my BabeAudit.com link to the Entertainment section! Thanks!
Perhaps you already noticed - I dont like doing sysadmin stuff using php On a linux system tmpreaper might be an option. It is specifically for purging old files from directories and excluding certain files. Thanks for the link offer bluemouse2. Unfortunately my choice of english sites is limited, so I added my blog "chengfu.net" to your web logs category.
thanks! both sites accepted as featured links I'll try the script later and let you know if I need something else.
the script works ok however when I try to exclude a file to be deleted I get errors (and they are still all deleted) do you have another idea how to exclude a specific file? thanks!
Could you paste the complete script you are using now? Or hjow you are defining the exclude files? If everything is correct sketch's solution should work fine.
If you want to test for a list of files, create an array with those files, then the last IF statement should be if (!in_array($donotdeletethese) AND filectime($file) < (time() - (12 * 3600))) {. how do I create an array with those files?
Like this: $donotdeletethese = array("file1.html","file2.html","someimg.jpg"); Make sure to put the filenames in quotes and separate each one with a comma.
I get this error: Wrong parameter count for in_array() in line: if (!in_array($donotdeletethese) AND filectime($file) < (time() - (1 * 3600))) {
That's because I'm a very dumb programmer ... Change $file = $dir.$file to $fullpath = $dir.$file then add $file, (with comma) after "in_array(", should work (it did here )
related to the last line: then add $file, (with comma) after "in_array(", should work (it did here ) can you please be more specific? thanks
This should be the complete code for sketch's proposal: <?php $dir = "/dir/"; # directory to check $dh = opendir($dir); while(false !== ($file = readdir($dh))) { $donotdeletethese = array("test.html","test.php"); $fullpath = $dir.$file; if (filetype($fullpath) != "file") continue; # Only files if (!in_array($file, $donotdeletethese) AND filectime($fullpath) < (time() - (1 * 3600))) { unlink($fullpath); # Delete file } } closedir($dh); ?> Code (markup):