PHP readdir() 不按字母顺序返回文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/541510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:02:31  来源:igfitidea点击:

PHP readdir() not returning files in alphabetical order

php

提问by Buggabill

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

我正在阅读一个包含一些图片的目录,例如使用非常简单的 readdir() 实现,如下所示:

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:

我遇到的问题是文件没有按字母顺序读取,因为 readdir() 状态的文档:

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.

另一个奇怪的事情是,在本地测试服务器上,相同的代码运行良好。这在两种情况下都在使用 LAMP 堆栈的服务器上运行。

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.

我知道我可以构建一个数组并对其进行排序,但我想知道我是否在做的事情中遗漏了什么。

回答by ZombieSheep

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.

'ls' 将按(通常)字母顺序显示文件的事实并不意味着它们在文件系统上的存储方式。恐怕 PHP 的行为符合规范。

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

如果必须按字母顺序排序,您可能需要考虑使用scandir作为您工作的基础。:)

回答by alex

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

您可以将所有文件名复制到一个数组中,然后使用

<?php
sort($filesArray);
?>

回答by SilentGhost

i suppose docs are quite clear here.

我想文档在这里很清楚。

order in which they are stored in filesystem

它们在文件系统中的存储顺序

is not the same as alphabetic order

与字母顺序不同

回答by diciu

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.

意味着文件按照它们的创建顺序返回。

回答by privateace

There are a couple you can use:

您可以使用以下几种方法:

Alphabetical Sort:

字母排序:

<?php
sort($handle);
?>

Reverse Alphabetical Sort:

逆字母排序:

<?php
rsort($handle);
?>