views:

3037

answers:

5

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!

+2  A: 

How about :

$directory = sort(scandir('Images'));
Evert
+1  A: 

You need to read your files into an array first before you can sort them. How about this?

<?php
$dirFiles = array();
// 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") {
         $dirFiles[] = $file;
        }
    }
    closedir($handle);
}

sort($dirFiles);
foreach($dirFiles as $file)
{
    echo "<li><a href=\"Images/$file\" class=\"thickbox\" rel=\"gallery\" title=\"$newstring\"><img src=\"Images/Thumbnails/$file\" alt=\"$newstring\" width=\"300\"  </a></li>\n";
}

?>

Edit: This isn't related to what you're asking, but you could get a more generic handling of file extensions with the pathinfo() function too. You wouldn't need a hard-coded array of extensions then, you could remove any extension.

zombat
Awesome - thank you so so much, especially for the extra tip, I'll look into that rightaway. This place is awesome, so fast! Also, my girlfriend (who's site it is for says thanks (see her newly sorted gallery here: http://joconlon.com/ !!)
felixthehat
Some nice work there!
David Caunt
A: 

$directory = scandir('Images');

A: 

Note: Move this into the foreach loop so that the newstring variable gets renamed correctly.

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

    $newstring = str_replace($crap, " ", $file );
Jason
A: 

Sorry to steal this thread, but thought best to add here first instead of posting a new question.

1st: I may be missing something, but the alphabetical ordering is not 100% e.g. I am using this to create a slideshow of a folder of items (to replace a PC running Powerpoint) and if I name the slides 01, 02, 03, 04 this works fine, but, if the slides are called Slide01, Slide02, Slide03, Slide04 ... Slide41 the ordering gets messed up.

2nd: As a work around (and this probably should be another post) but could the opendir command scan subdirectories so for example:
..\show01\01.jpg, 02.jpg, 03.jpg
..\show02\01.jpg, 02.jpg, 03.jpg

theJonson