php script to delete files older than 24 hrs, deletes all files



PHP Snippet 1:

(time()-filectime($path.$file)) < 86400

PHP Snippet 2:

 if (preg_match('/\.pdf$/i', $file)) {
     unlink($path.$file);
 }

PHP Snippet 3:

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

?>

PHP Snippet 4:

<?php   
$dir = getcwd()."/temp/";//dir absolute path
$interval = strtotime('-24 hours');//files older than 24hours

foreach (glob($dir."*") as $file) 
    //delete if older
    if (filemtime($file) <= $interval ) unlink($file);?>

PHP Snippet 5:

$path = dirname(__FILE__);
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$timer = 300;
$filetime = filectime($file)+$timer;
$time = time();
$count = $time-$filetime;
    if($count >= 0) {
      if (preg_match('/\.png$/i', $file)) {
        unlink($path.'/'.$file);
      }
    }
}
}

PHP Snippet 6:

$path='cache/';
$cache_max_age=86400; # 24h
if($handle=opendir($path)){
    while($file=readdir($handle)){
        if(substr($file,-6)=='.cache'){
            $filectime=filectime($path.$file);
            if($filectime and $filectime+$cache_max_age<time()){
                unlink($path.'/'.$file);
            }
        }
    }
}

PHP Snippet 7:

$path='cache/';
$cache_max_age= 86400; # 24h
foreach(glob($path."*.cache") as $file){
    $filectime=filectime($file);
    if($filectime and $filectime+$cache_max_age<time()){
        unlink($file);
    }
}