Click here to Skip to main content
15,884,962 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there, I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.

What I have tried:

PHP
<?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>";
}
?>
Posted
Updated 26-Sep-22 3:49am
Comments
Member 15627495 23-Sep-22 16:03pm    
in php, the function is_file($entry) return true when checking a file.
it avoid to test '.' , '..'


$temp="";
while ( ..... ){
// use concatenation for html in a temporary var
 if (is_file($entry)){
 $temp .= "<li> ......</li>
 }
}
echo "<ul>".$temp."</ul><div>........" :

1 solution

The entries are returned in the order in which they are stored by the filesystem.
If your filesystem doesn't store the entries alphabetically, then try:
sorting_order
By default, the sorted order is alphabetical in ascending order. If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. If it is set to SCANDIR_SORT_NONE then the result is unsorted.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900