19

I am reading through a directory with some pictures and such using a pretty simple implementation of readdir() like the following:

if ($handle = opendir($path)) {
    while (false !== ($szFilename = readdir($handle))) {
    if ($szFilename[0] !== '.') {
        if (is_file($path.$szFilename)) {
                // do stuff
            }
        }
     }
 }

The problem that I am having is that the files are not being read in alphabetical order as the docs for readdir() state:

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

Another weird thing is that, on the local testing server, the same code works great. This is running on a server using the LAMP stack in both cases.

I know that I can build an array and just sort it, but I was wondering if I was missing something in what I was doing.

alex
  • 479,566
  • 201
  • 878
  • 984
Buggabill
  • 13,726
  • 4
  • 42
  • 47

5 Answers5

32

Alphabetical order :: I think you misread the snippet you quoted...

Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.

The fact that 'ls' would display the files in (usually) alphabetical order does not mean that's how they are stored on the filesystem. PHP is behaving as spec, I'm afraid.

You may want to consider using scandir as the basis for your efforts, if alphabetical sorting is a must. :)

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
14

You could copy all the filenames into an array and then use

<?php
sort($filesArray);
?>
alex
  • 479,566
  • 201
  • 878
  • 984
4

i suppose docs are quite clear here.

order in which they are stored in filesystem

is not the same as alphabetic order

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • maybe he stored the images on your testserver in alphabetical order, thereby making stored order equal to alphabetical order – Jacco Feb 12 '09 at 14:38
  • order is not guaranteed to be alphabetic. that's what docs say. – SilentGhost Feb 12 '09 at 14:41
  • The docs do not say that the order is not guaranteed to be alphabetic. I wouldn't say they're "quite clear" about file-system order being different from alphabetical order. The docs say nothing about alphabetical. They could be clearer about the difference. – Rob Kennedy Feb 12 '09 at 15:09
  • ok, let me repharse. Docs don't say that the order is guaranteed to be alphabetic, in fact citation doesn't say anything about alphabetic. And that is clear as a daylight. – SilentGhost Feb 12 '09 at 15:45
  • Being tacit on a subject never makes something "clear as daylight." The docs could say, "File-system order is not necessarily the same as commonly expected orderings such as alphabetical or creation-time order." THAT would be clear. – Rob Kennedy Feb 12 '09 at 22:07
1

You're misreading the docs:

The filenames are returned in the order in which they are stored by the filesystem.

means that files are returned in the order they were created.

diciu
  • 29,133
  • 4
  • 51
  • 68
  • 1
    nitpick: _probably_ means the order they were created =) it's possible to create a filesystem that returns the files in some other order, I'd imagine. – gnud Feb 12 '09 at 14:40
-3

There are a couple you can use:

Alphabetical Sort:

<?php
sort($handle);
?>

Reverse Alphabetical Sort:

<?php
rsort($handle);
?>
alex
  • 479,566
  • 201
  • 878
  • 984
privateace
  • 1,367
  • 6
  • 16
  • 24
  • `$handle` in the OP is a [`resource`](http://php.net/manual/en/language.types.resource.php), which neither [`sort`](http://php.net/manual/en/function.sort.php) nor [`rsort`](http://php.net/manual/en/function.rsort.php) accept. One would need to pull the entire array into memory and then call `sort()` or `rsort()`. If one bothers to do that, one might as well use `scandir` in all modern PHP implementations. – bishop Sep 18 '15 at 17:36