Tuesday, May 8, 2012

How to delete all files older than 10 days with PhP?

$basedir = BASE_DIRECTORY . "/logs/";
$count = 0;

$files = glob($basedir . "/*");
if(!empty($files)){
    foreach($files as $file) {
        if(is_file($file) && time() - filemtime($file) >= 10*24*60*60) { // 10 days
            unlink($file);
            $count++;
            //print $file."\n";
        }
    }
}
print "\n -------- Files deleted : $count ---------- \n";die;

No comments:

Post a Comment