Here you go. Hasn't been tested:
PHP Code:
$dir = '/www/';
$Files=array();
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file[0] == '.' || is_dir("$dir/$file")) {
continue;
}
if ((time() - filemtime($file)) > ($days *86400)) { //7 days
//unlink("$dir/$file");
$Files[$dir/$file]=filemtime($file);
}
}
closedir($handle);
//Sort the array by file modified times, reverse sort
arsort($Files);
//Remove the newest 3 files from the array
$Files=array_slice($Files, 3);
//Remove the remainder of files
foreach($Files as $FilePath=>$ModifiedTime)
{
unlink($FilePath);
}
}