Search code examples
phpalphabeticalopendirscandir

Sort alphabetically with opendir()


I'm quite new to PHP so I'm still learning the very basics, however I'm trying to create an image gallery.

After countless Google searches later, I found a PHP script that does what I want it to do, and after looking at the code and manipulating it slightly it was working perfectly with my site; except that the images were not in alphabetical order.

This is the code

$max_width = 100;
$max_height = 100;
$imagedir = 'gifs/animals/'; //Remember trailing slash


function getPictureType($ext) {
    if ( preg_match('/jpg|jpeg/i', $ext) ) {
        return 'jpg';
    } else if ( preg_match('/png/i', $ext) ) {
        return 'png';
    } else if ( preg_match('/gif/i', $ext) ) {
        return 'gif';
    } else {
        return '';
    }
}

function getPictures() {
    global $max_width, $max_height, $imagedir;
    if ( $handle = opendir($imagedir) ) {
        $lightbox = rand();
        echo '<ul id="pictures">';
        while ( ($file = readdir($handle)) !== false ) {
            if ( !is_dir($file) ) {
                $split = explode($imagedir, $file); 
                $ext = $split[count($split) - 1];
                if ( ($type = getPictureType($ext)) == '' ) {
                    continue;
                }

                $name = substr($file, 0, -4);
                $title = str_replace("_"," ",$name);
                echo '<li><a href="'.$name.'">';
                echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
                echo '</a>';
                echo ''.$title.'';
                echo '</li>';
            }
        }
        echo '</ul>';

    }
}

I've used the scandir() function which works in sorting them alphabetically, however I was left with an array. I then used the implode function to join the array together, however after that I was stuck with what to do.

Any help would be greatly appreciated!

Cheers.


Solution

  • What's wrong with the arrays? Also it would be better if you use pathinfo to obtain the filename and the extension.

    $max_width = 100;
    $max_height = 100;
    $imagedir = 'gifs/animals/'; //Remember trailing slash
    
    
    function getPictureType($ext) {
        if ( preg_match('/jpg|jpeg/i', $ext) ) {
            return 'jpg';
        } else if ( preg_match('/png/i', $ext) ) {
            return 'png';
        } else if ( preg_match('/gif/i', $ext) ) {
            return 'gif';
        } else {
            return '';
        }
    }
    
    function getPictures() {
        global $max_width, $max_height, $imagedir;
        if ( $files = scandir($imagedir) ) {
            $lightbox = rand();
            echo '<ul id="pictures">';
            foreach ($files as $file) {
                $full_path = $imagedir.'/'.$file;
                if ( !is_dir($file) ) {
                    $finfo = pathinfo($full_path); 
                    $ext = $finfo['extension'];
                    if ( ($type = getPictureType($ext)) == '' ) {
                        continue;
                    }
    
                    $name = $finfo['filename'];
                    $title = str_replace("_"," ",$name);
                    echo '<li><a href="'.$name.'">';
                    echo '<img src="thumbs/'.$file.'" class="pictures" alt="'.$file.'" />';
                    echo '</a>';
                    echo ''.$title.'';
                    echo '</li>';
                }
            }
            echo '</ul>';
    
        }
    }