开发者

How to delete photos that haven't been accessed for more than 5 days?

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);  
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜