Yeti
Yeti

Reputation: 5818

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 not a problem, but how do I go about writing the script which will take care of the deletion?

Upvotes: 0

Views: 265

Answers (1)

jasonbar
jasonbar

Reputation: 13461

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

Upvotes: 3

Related Questions