I am looking for a way to auto delete files in a certain folder if they have not been accessed in X amount of hours. For example, if a certain file has not been accessed within 8 hours, it should be automatically deleted. I am using a cPanel server on a shared hosting account.
You need to use a cron job on the server that runs a script to check the last access date of all the files in the folder and delete old files.
Here's the code to check the files (untested). You just need to test it out and if it works create a cron job to run in at whatever interval you like. <?php $age = 3600; //Set how old a file must be to delete, in seconds (3600 secs is 10 mins) $time = time(); // Get current time $dir = '/path/to/files'; // set directory of files to be checked //Check for files if ($handle = opendir($dir)) { //loop through files while (false !== ($file = readdir($handle))) { $lastAccess = fileatime($file); //Get last access time if( ($time - $lastAccess) >= $age ) //check age unset($file); // delete old files } closedir($handle); } ?> PHP:
I just tried running that code and got about 1000 lines of the following error: Warning: fileatime() [function.fileatime]: stat failed for FileToBeDelete.html in /home/public_html/mydomain.com/cron.php on line 10
Try this script: <?php $age = 3600; //Set how old a file must be to delete, in seconds (3600 secs is 10 mins) $time = time(); // Get current time $dir = '/path/to/files'; // set directory of files to be checked, no trailing slash //Check for files if ($handle = opendir($dir)) { //loop through files while (false !== ($file = readdir($handle))) { $filepath = $dir . '/' . $file; $lastAccess = fileatime($filepath); //Get last access time if( $lastAccess !== false ) // if fileatime didn't fail if( ($time - $lastAccess) >= $age ) //check age unset($filepath); // delete old files } closedir($handle); } ?> PHP:
Ah yes I did. Sorry about that. unset() destroys the variable, unlink() deletes the file. Thanks for catching that Qc4
I wouldn't try it on your server. Make a small folder with 10 files or so. if you got 1000 errors, that means the script looped 1000 times, aka 1000 files. are you sure you want all those to be deleted? Yes, do a cronjobs. it will run your script periodically, however you set it up.
Sorry but your code won't work. You might want to go over how loops and increments work in php. '$mdel = $i++;' should be '$mdel++;' and it must be inside the loop (in the check age if statement block) to increment each time a file is deleted. Also, why add a limit the loop when no limit was requested?
Here's the updated code. <?php $age = 28800; //Set how old a file must be to delete, in seconds (28800 secs is 8 hours) $time = time(); // Get current time $dir = '/path/to/files'; // set directory of files to be checked, no trailing slash //Check for files if ($handle = opendir($dir)) {   //loop through files   while (false !== ($file = readdir($handle))) {     $filepath = $dir . '/' . $file;     $lastAccess = fileatime($filepath); //Get last access time     if( $lastAccess !== false ) // if fileatime didn't fail       if( ($time - $lastAccess) >= $age ) //check age         unlink($filepath); // delete old files   }   closedir($handle); } ?> PHP:
Well apart from not initializing $i it's a valid code but it doesn't have your desired effect. The code you posted will limit the number of files tested to just 10 files rather than limit the number of files deleted to 10. HOWEVER in this case a limit is NOT a desired effect. If a limit was present then the number of old files in the directory would build up and cause the whole script (and the site running the script) to slow down. In this case you would want all old files, regardless of how many there are, to be deleted. I think it would be best if we continue this php lesson in PMs as it has taken over this thread (which is against the rules). EDIT: Oh I didn't catch it before but unset() should be unlink(). Thx Qc4
Instead of looping continuously we can set 1000 as a maximum number then it will be enough and prevents server load too. Alternative is to schedule using the cron jobs when there a large number of files