Nikopol Nikopol - 2 months ago 11
PHP Question

How to make last uploaded images appear first in foreach loop

I have this code that shows uploaded images:

<?php
$dir = "img/*.jpg";
$images = glob( $dir );

foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>


but I need my last uploaded image to appear first in line. I know it's right there in front of me, but I can't see it. How do I do that?

Answer

Try reversing the array before your foreach.

<?php    
$dir = "img/*.jpg";
$images = glob( $dir );
$images = array_reverse($images);
foreach( $images as $image ):

echo "<img src='" . $image . "' />";    

endforeach;

?>
Comments