How to list files in a directory in "last modified date" order? (PHP5 on Linux)
function newest($a, $b)
{
return filemtime($a) - filemtime($b);
}
$dir = glob('files/*'); // put all files in an array
uasort($dir, "newest"); // sort the array by calling newest()
foreach($dir as $file)
{
echo basename($file).'<br />';
}
Credit goes here.
read files in a directory by using readdir
to an array along with their filemtime
saved. Sort the array based on this value, and you get the results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With