PHP readdir() not returning files in alphabetical order

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. 🙂

Leave a Comment