Sort and display directory list alphabetically using opendir() in php

Posted by felixthehat on Stack Overflow See other posts from Stack Overflow or by felixthehat
Published on 2009-05-19T20:55:28Z Indexed on 2010/03/17 2:11 UTC
Read the original article Hit count: 322

Filed under:
|
|

Hi there, php noob here - I've cobbled together this script to display a list of images from a folder with opendir, but I can't work out how (or where) to sort the array alphabetically

<?php

// opens images folder
if ($handle = opendir('Images')) {
while (false !== ($file = readdir($handle))) {

// strips files extensions	
$crap   = array(".jpg", ".jpeg", ".JPG", ".JPEG", ".png", ".PNG", ".gif", ".GIF", ".bmp", ".BMP", "_", "-");	

$newstring = str_replace($crap, " ", $file ); 	

//asort($file, SORT_NUMERIC); - doesnt work :(

// hides folders, writes out ul of images and thumbnails from two folders

    if ($file != "." && $file != ".." && $file != "index.php" && $file != "Thumbnails") {
    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";}
}
closedir($handle);
}

?>

Any advice or pointers would be much appreciated!

© Stack Overflow or respective owner

Related posts about php

Related posts about sorting