Reputation: 4355
Here is the code I use to get an array of the files in a directory.
$dir = "thumbnails/";
$images = scandir($dir);
How can I sort the $images array by creation date? I found a couple of ways but I couldn't get any of them to work with my array.
Thanks,
Upvotes: 1
Views: 3249
Reputation: 979
You have to sort manually
$dir = "thumbnails/";
function compare_time($a, $b)
{
global $dir;
$timeA = filectime("$dir/$a");
$timeB = filectime("$dir/$b");
if($timeA == $timeB) return 0;
return ($timeA < $timeB) ? -1 : 1;
}
$images = scandir($dir);
usort($images, 'compare_time');
Upvotes: 0
Reputation: 13599
try the following tutorial that includes a script on how to sort by file modification/creation.
http://www.bitrepository.com/sort-files-by-filemtime.html
Upvotes: 0
Reputation: 799390
On Windows you can get the creation time of the file with filectime()
. Just put it in an array with the filename and sort the array.
Creation time is not stored on most *nix filesystems.
Upvotes: 1