我正在阅读一个包含一些图片的目录,例如使用 readdir() 的一个非常简单的实现,如下所示:
if ($handle = opendir($path)) {
while (false !== ($szFilename = readdir($handle))) {
if ($szFilename[0] !== '.') {
if (is_file($path.$szFilename)) {
// do stuff
}
}
}
}
我遇到的问题是文件没有像 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.
另一件奇怪的事情是,在本地测试服务器上,相同的代码运行良好。在这两种情况下,它都在使用 LAMP 堆栈的服务器上运行。
我知道我可以构建一个数组并对其进行排序,但我想知道我在做的事情中是否遗漏了什么。
最佳答案
字母顺序::我认为你误读了你引用的片段......
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.
“ls”会按(通常)字母顺序显示文件的事实并不意味着它们在文件系统中的存储方式。恐怕 PHP 的行为符合规范。
您可能需要考虑使用 scandir作为你努力的基础,如果按字母顺序排序是必须的。 :)
关于PHP readdir()不按字母顺序返回文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/541510/