Reputation: 89
I've written some PHP to call all images from a directory and display them on a HTML page. As the file names are randomly generated to be identified for our online inventory system, I need them to be displayed in order they were uploaded to the system.
Below is the code I have written so far, am I correct in thinking it needs an if statement adding to it? Any help appreciated!
$dirname = "../dashboard/uploads/$inventory/";
$images = glob($dirname."*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'" class="print-images">';
}
Upvotes: 0
Views: 607
Reputation: 1968
You can try something like this:
$dirname = "../dashboard/uploads/$inventory/";
$images = glob($dirname."*.jpg");
$items = array();
foreach($images as $image) {
$item = new stdClass;
$item->time = filemtime($image);
$item->image = $image;
$items[] = $item;
}
function sortCallback($a,$b){
if ($a->time > $b->time) return 1;
if ($a->time < $b->time) return -1;
return 0;
}
usort($items, "sortCallback");
foreach($items as $item) {
echo '<img src="'.$item->image.'" class="print-images">';
}
You can change filemtime function to filectime if you want date when image is created, also in sortCallback function you can change sort order.
It' may contain errors as I have not tested it.
Upvotes: 1