PHP readdir() not returning files in alphabetical order

后端 未结 5 2079
感动是毒
感动是毒 2020-11-27 08:46

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

if ($handle = opendir($pat         


        
相关标签:
5条回答
  • 2020-11-27 08:52

    i suppose docs are quite clear here.

    order in which they are stored in filesystem

    is not the same as alphabetic order

    0 讨论(0)
  • 2020-11-27 08:56

    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. :)

    0 讨论(0)
  • 2020-11-27 09:01

    There are a couple you can use:

    Alphabetical Sort:

    <?php
    sort($handle);
    ?>
    

    Reverse Alphabetical Sort:

    <?php
    rsort($handle);
    ?>
    
    0 讨论(0)
  • 2020-11-27 09:05

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

    <?php
    sort($filesArray);
    ?>
    
    0 讨论(0)
  • 2020-11-27 09:08

    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.

    0 讨论(0)
提交回复
热议问题