I'm trying to create a cron job which will automatically delete .jpg files from a particular folder that haven't been accessed for more than 5 days. Running the cron job is开发者_JAVA技巧 not a problem, but how do I go about writing the script which will take care of the deletion?
Assuming your filesystem is mounted with atime / relatime options you can use fileatime() to detect the last access time.
So something like:
$dir = '/your/path/';
if ($fh = opendir($dir))
{
while(($file = readdir($fh)) !== FALSE)
{
if ($file == '.' || $file == '..')
continue;
if (is_file($dir . $file) && fileatime($dir . $file) < strtotime('-5 days'))
unlink($dir . $file);
}
closedir($fh);
}
精彩评论