What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
If images in a PHP gallery appear in the wrong order, check the array of paths immediately before the gallery loop. PHP’s scandir() sorts entries alphabetically in ascending order by default—not by upload time, photo date, or the order a file manager displays. Filter the directory contents to images, then explicitly sort them by the sequence your gallery is meant to use.
Why directory images appear out of order
A directory listing does not inherently represent a gallery sequence. In particular, scandir() returns entries in alphabetical ascending order by default, as the PHP manual documents. That can differ from upload order, capture chronology, or a manually arranged sequence. For example, alphabetical sorting can place photo10.jpg before photo2.jpg, because the names are compared as text.
scandir() also returns directory entries, not just gallery images. Its results can include ., .., subdirectories, and files your gallery should not display. Decide both which files belong in the gallery and what “in order” means before rendering them.
Inspect the array immediately before rendering
Print or log the final array of image paths immediately before the gallery loop. Check that it contains only intended images and compare its sequence with the order you want.
Recommended Free Tools
#1 Best Overall
$images = /* your filtered and sorted image paths */;
var_dump($images);
foreach ($images as $image) {
// Render the image.
}
If the array is already in the intended order, investigate the rendering loop or any client-side code that rearranges the images. If it is wrong there, correct the filtering or sorting step that builds the array.
Filter directory entries to supported images
Remove the special directory entries and any subdirectories, then keep only extensions your gallery supports. For example:
Rank #2
$directory = __DIR__ . '/images';
$entries = scandir($directory);
if ($entries === false) {
throw new RuntimeException('Could not read the gallery directory.');
}
$images = [];
$supportedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'webp'];
foreach ($entries as $entry) {
if ($entry === '.' || $entry === '..') {
continue;
}
$path = $directory . DIRECTORY_SEPARATOR . $entry;
if (!is_file($path)) {
continue;
}
$extension = strtolower(pathinfo($entry, PATHINFO_EXTENSION));
if (in_array($extension, $supportedExtensions, true)) {
$images[] = $entry;
}
}
Adjust the extension list to match formats your gallery actually renders. This example filters filenames by extension; it does not verify that a file’s contents are a valid image.
Choose a sorting rule that matches the gallery
| Ordering rule | Use it when | Important detail |
|---|---|---|
| Alphabetical | Filenames themselves define the order. | Names with numbers are compared as text, so photo10.jpg may come before photo2.jpg. |
| Numeric or natural filename order | Filenames contain sequence numbers. | Use a consistent naming scheme; zero-padding such as photo01.jpg, photo02.jpg, and photo10.jpg also makes ordinary alphabetical sorting predictable. |
| Timestamp | The gallery should show a chronology. | Choose the timestamp deliberately: file modification time, upload time, and EXIF capture time are not interchangeable. |
| Saved sequence | The gallery owner needs a specific editorial order. | Store an order value with each image and sort by it; this is more reliable than depending on filesystem enumeration. |
Alphabetical or natural filename order
To sort filtered filenames alphabetically in ascending order, use sort($images, SORT_STRING). For filenames with embedded numbers, PHP’s natural, case-insensitive comparison can produce the expected numeric sequence:
usort($images, 'strnatcasecmp');
Natural sorting follows the parts of the names, so a consistent naming convention still matters. If filenames include other text or numbers that should not affect placement, use a comparator tailored to the sequence encoded in your names.
Timestamp order
For a modification-time order, sort using filemtime() on each full path. This orders by the files’ recorded modification times; editing or copying files can change those times, so they may not reflect when a photo was uploaded or taken. If chronology must represent upload time, store that value when the image is uploaded. If it must represent camera capture time, read and store the relevant EXIF date where available.
Rank #4
Explicit editorial order
If people expect a hand-picked sequence, save a position for each image in a database or other gallery metadata and sort by that value. That lets the owner rearrange images without renaming files or relying on timestamps.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Do not use unsorted enumeration to create a meaningful order
The PHP manual documents SCANDIR_SORT_NONE for unsorted output. That option does not mean upload order or any other dependable gallery sequence; it only opts out of scandir()’s default sort. Use it only when the order genuinely does not matter, and sort explicitly whenever visitors need a predictable sequence.
Check for directory-reading failures
scandir() returns an array of filenames on success and false on failure. If the supplied path is not a directory, it returns false and emits an E_WARNING, according to the PHP manual. Check the configured path and handle the failure before treating a missing gallery as a sorting problem.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

