How would i delete all the images in a folder that are 24 hours old or older [duplicate]
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 {} \;');
精彩评论