Friday, 23 January 2015

Find the oldest file in directory - PHP

<?php
function getOldestFile($directory) {
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if (is_file($directory.$file)) {
$files[] = $file;
}
}
if (count($files) <= 12) {
return;
} else {
foreach ($files as $val) {
if (is_file($directory.$val)) {
$file_date[$val] = filemtime($directory.$val);
}
}
}
}
closedir($handle);
asort($file_date, SORT_NUMERIC);
reset($file_date);
$oldest = key($file_date);
return $oldest;
}
echo getOldestFile("/path/to/your/directory/", 8);
?>

0 comments:

Post a Comment