How do I sort pictures alphabetically in gallery?

Viewed 18

I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.

<?php

$dir = htmlspecialchars($_GET["dir"]);
$className = htmlspecialchars($_GET["className"]);

if ($handle = opendir($dir)) {
    echo "<div class='photogallery $className'><ul>";
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
            $path = $dir."/".$entry;
            $pathThumb = $dir."/thumbs/".$entry;

            echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
                </li>";
        }
    }
    closedir($handle);
    echo "</ul><div class='both'></div></div>";
}
?>
1 Answers

glob returns the directory sorted unless you specify otherwise.


echo "<div class='photogallery $className'><ul>";
foreach (glob($dir . "/*.*") as $entry) {
    if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
        $path = $dir . "/" . $entry;
        $pathThumb = $dir . "/thumbs/" . $entry;

        echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
            </li>";
    }
}
echo "</ul><div class='both'></div></div>";
Related