简体   繁体   English

PHP readdir / opendir如何排序

[英]How does PHP readdir/opendir sort

I wondered how come that when calling following code the file(name)s in the array are always sorted differently. 我想知道为什么在调用以下代码时数组中的文件(名称)总是以不同的顺序排序。 How does PHP opendir sort the files and how can I change it in the system without having to put it inside an array first which I then sort? PHP opendir如何对文件进行排序,如何在系统中更改文件而不必先将其放入数组中然后进行排序?

$dh  = opendir($dir);  
    do {
        $files_in_dir[] = $filename;
    }
    while (false !== ($filename = readdir($dh)));

As a suggestion, use PHP 's scandir() function instead, as PHP 's official document defines it: 作为建议,请改用PHPscandir()函数,因为PHP的官方文档对其进行了定义:

scandir — List files and directories inside the specified path scandir-列出指定路径内的文件和目录

And also supports sorting. 并且还支持排序。

By default, the sorted order is alphabetical in ascending order. 默认情况下,排序顺序按字母升序排列。 If the optional sorting_order is set to SCANDIR_SORT_DESCENDING, then the sort order is alphabetical in descending order. 如果将可选的sorting_order设置为SCANDIR_SORT_DESCENDING,则排序顺序按字母降序排列。 If it is set to SCANDIR_SORT_NONE then the result is unsorted. 如果将其设置为SCANDIR_SORT_NONE,则结果将不排序。

The entries are returned in the order in which they are stored by the filesystem. 条目按文件系统存储的顺序返回。

http://php.net/manual/en/function.readdir.php http://php.net/manual/en/function.readdir.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM