开发者

How would i delete all the images in a folder that are 24 hours old or older [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to delete files from directory based on creation date in php?

How would i delete all the images in a folder that are 24 hours old or older in p开发者_StackOverflow中文版hp?


$imagePattern = "/\.(jpg|jpeg|png|gif|bmp|tiff)$/";
$directory = ".";

if (($handle = opendir($directory)) != false) {
    while (($file = readdir($handle)) != false) {
        $filename = "$directory/$file";
        if (strtotime("-24 hours") <= filemtime($filename) && preg_match($imagePattern, $filename)) {
            unlink($filename);
        }
    }

    closedir($handle);
}


If you're on *nix, punt it off to the shell and find:

shell_exec('find /path/to/your/directory -mtime +0 -exec rm -f {} \;');


An alternate solution would be to use a naming convention that includes a unix timestamp if you have control over that.


combination of:

to check time http://us2.php.net/manual/en/function.filemtime.php

to delete http://us2.php.net/manual/en/function.unlink.php


shell_exec('find /path/to/files* -mtime +1 -exec rm {} \;');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜