Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP (folder) File Listing in Alphabetical Order?

I'm not sure how simple this would be, but I'm using a script which displays the files from a specific folder, however I'd like them to be displayed in alphabetical order, would it be hard to do this? Here's the code I'm using:

if ($handle = opendir($mainframe->getCfg( 'absolute_path' ) ."/images/store/")) {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..")  {
                if (($file != "index.html")&&($file != "index.php")&&($file != "Thumbs.db")) {
                $strExt = end(explode(".", $file));
                    if ($strExt == 'jpg') {
                        $Link = 'index.php?option=com_shop&task=deleteFile&file[]='.$file;
                        $thelist .= '<tr class="row0"><td nowrap="nowrap"><a href="'.$Link.'">'.$file.'</a></td>'."\n";
                        $thelist .= '<td align="center" class="order"><a href="'.$Link.'" title="delete"><img src="/administrator/images/publish_x.png" width="16" height="16" alt="delete"></a></td></tr>'."\n";
                    }

                }
            }
        }
        closedir($handle); 
    }   
    echo $thelist;

:)

like image 936
SoulieBaby Avatar asked Jul 08 '09 01:07

SoulieBaby


3 Answers

Instead of using readdir you could simply use scandir (documentation) which sorts alphabetically by default.

The return value of scandir is an array instead of a string, so your code would have to be adjusted slightly, to iterate over the array instead of checking for the final null return value. Also, scandir takes a string with the directory path instead of a file handle as input, the new version would look something like this:

foreach(scandir($mainframe->getCfg( 'absolute_path' ) ."/images/store/") as $file) {
  // rest of the loop could remain unchanged
}
like image 107
Christian Hang-Hicks Avatar answered Sep 20 '22 09:09

Christian Hang-Hicks


That code looks pretty messy. You can separate the directory traversing logic with the presentation. A much more concise version (in my opinion):

<?php

// Head of page
$it = new DirectoryIterator($mainframe->getCfg('absolute_path') . '/images/store/'));

foreach ($it as $file) {
    if (preg_match('#\.jpe?g$#', $file->getFilename()))
        $files[] = $file->getFilename();
}
sort($files);

// Further down
foreach ($files as $file)
    // display links to delete file.
?>

You don't even need to worry about opening or closing the handle, and since you're checking the filename with a regular expression, you don't need any of the explode or conditional checks.

like image 26
Nick Presta Avatar answered Sep 19 '22 09:09

Nick Presta


I like Glob It makes directory reading a snap as it returns an array that's easily sortable:

<?php
$files = glob("*.txt");
sort($files);
foreach ($files as $filename) {
    echo "$filename size " . filesize($filename) . "\n";
}
?>
like image 33
SeanDowney Avatar answered Sep 20 '22 09:09

SeanDowney