You can do it like this to sort either alphabetically or by file time (tested):
$directory = '/mydirectory/pdf';
$sort_field = 'time'; // Or 'file'
$file_array = get_file_array($directory);
$file_array = sort_multi_array($file_array, $sort_field);
foreach ($file_array as $value) {
echo $value['file'] . ' => ' . $value['time'] . '<br />';
}
function get_file_array($dir)
{
if (substr($dir, -1) != '/') {
$dir .= '/';
}
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
$time = filemtime($dir . $file);
$file_arr[] = array('file' => $file, 'time' => $time);
}
}
closedir($handle);
return $file_arr;
} else {
return false;
}
}
function sort_multi_array($array, $sort_key, $direction = SORT_ASC) // Or SORT_DESC
{
foreach ($array as $key => $value) {
$sort_arr[] = $value[$sort_key];
}
array_multisort($sort_arr, $direction, $array);
return $array;
}