I'm creating an image gallery and would like for my most recent uploaded images to be at the front.
This is what I currently have:
$files = glob("images/*.*");
for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
$supported_file = array('gif','jpg','jpeg','png');
$ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
if (in_array($ext, $supported_file)) {
echo basename($image)."<br />"; // show only image name if you want to show full path then use this code // echo $image."<br />";
echo '<img src="'.$image .'" alt="Random image" />'."<br /><br />";
} else {
continue;
}
}
But I'm not sure how to make it display in order of last uploaded.
rsort()
Will that work for you? – mickmackusa Jul 4 '17 at 22:40glob()
results then sorting. There are previous answers on SO that state how to sort the files: stackoverflow.com/questions/2667065/sort-files-by-date-in-php and stackoverflow.com/questions/13572883/… . The glob() filtering is here: stackoverflow.com/questions/20929025/… – mickmackusa Jul 4 '17 at 23:27