php script to delete files older than 24 hrs, deletes all files
<?php
/** define the directory **/
$dir = "images/temp/";
/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {
/*** if file is 24 hours (86400 seconds) old then delete it ***/
if(time() - filectime($file) > 86400){
unlink($file);
}
}
?>
You can also specify file type by adding an extension after the * (wildcard) eg
For jpg images use: glob($dir."*.jpg")
For txt files use: glob($dir."*.txt")
For htm files use: glob($dir."*.htm")
(time()-filectime($path.$file)) < 86400
If the current time and file's changed time are within 86400 seconds of each other, then...
if (preg_match('/\.pdf$/i', $file)) {
unlink($path.$file);
}
I think that may be your problem. Change it to > or >= and it should work correctly.
- You want
>
instead. - Unless you're running on Windows, you want
filemtime()
instead.