I'm using the following PHP script to show me the images contained in a directory..
$imagetypes = ['image/jpeg', 'image/gif', 'image/png'];
function getImages($dir)
{
// array to hold return value
$retval = [];
// add trailing slash if missing
if(substr($dir, -1) != "/") {
$dir .= "/";
}
// full server path to directory
$fulldir = "{$_SERVER['DOCUMENT_ROOT']}/$dir";
$d = @dir($fulldir) or die("getImages: Failed opening directory {$dir} for reading");
while(FALSE !== ($entry = $d->read())) {
// skip hidden files
if($entry{0} == ".") continue;
// check for image files
$f = escapeshellarg("{$fulldir}{$entry}");
$mimetype = trim(shell_exec("file -bi {$f}"));
foreach($GLOBALS['imagetypes'] as $valid_type) {
if(preg_match("@^{$valid_type}@", $mimetype)) {
$retval[] = [
'file' => "/{$dir}{$entry}",
'size' => getimagesize("{$fulldir}{$entry}")
];
break;
}
}
}
$d->close();
return $retval;
}
// fetch image details
$images = getImages("imguploader/UploadFolder");
// display on page
foreach($images as $img) {
echo "<img class=\"photo\" src=\"{$img['file']}\" {$img['size'][3]} alt=\"\">\n";
}
This is all working very nicely and the only thing I'd now like to do is to have them display in filename alphabetical order instead of the seemingly random order they appear in currently.