Ok, here we go, a PHP script that deletes files that are X number of days.
<? $days = 1; $dir = dirname ( __FILE__ ); $nofiles = 0; if ($handle = opendir($dir)) { while (( $file = readdir($handle)) !== false ) { if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) { continue; } if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) { $nofiles++; unlink($dir.'/'.$file); } } closedir($handle); echo "Total files deleted: $nofiles \n"; } ?>
Now paste this code and save it as a php file, upload it to the folder where you want to delete the files. You can see at the beginning of this php code
$days = 1;
which sets the number of days, for example, if you set it to 2, files that are older than 2 days will be deleted. Basically this is what happens when the script starts, gets the current directory and reads the file entries, skips'. for the current directory and further checks if there are other directories,
if ( $file == '.' || $file == '..' || is_dir($dir.'/'.$file) ) { continue; }
if the file entry is not a directory, then it retrieves the modified file time (last modified time) and compares if this is the number of days
if ((time() - filemtime($dir.'/'.$file)) > ($days *86400)) { $nofiles++; unlink($dir.'/'.$file); }
if the condition becomes true, it deletes the file using the unlink () php function. Finally closes the directory and exits. I also added a counter to count the number of files to be deleted that will be displayed at the end of the deletion process. So put the php file in the directory that needs to delete the file and execute it.
Hope this helps :)
Raymond
source share