I tested two methods to compare the speed. Option A was repeatedly about 50% faster. I ran this on a folder with about 6000 files.
Option A
$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);
}
}
}
}
Option B
$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);
}
}
It also checks whether a file creation time was returned. On some systems, there are problems with returning the creation time. Therefore I wanted to make sure that it does not delete all files if the system does not return a timestamp.