You can use the filemtime()
function to find the modification date of each file. This can be used as a key to sort the array using uksort()
before it is processed in the loop.
This will put the array in ascending order of file modification time, i.e. those with the earliest mtime first. You can then either reverse the array, or iterate through it backwards.
<?php
function mtimecmp($a, $b) {
$mt_a = filemtime($a);
$mt_b = filemtime($b);
if ($mt_a == $mt_b)
return 0;
else if ($mt_a < $mt_b)
return -1;
else
return 1;
}
$images = glob($dirname."*.jpg");
usort($images, "mtimecmp");
$images=array_reverse($images);
foreach ($images as $image) {
echo '<img src="'.$image.'" height ="400"/><br />';
}
?>
(Iterating backwards is more efficient...)
// ...
usort($images, "mtimecmp");
for ($i = count($images) - 1; $i >= 0; $i--) {
$image = $images[$i];
echo '<img src="'.$image.'" height ="400"/><br />';
}