PHP readdir()不按字母顺序返回文件 [英] PHP readdir() not returning files in alphabetical order

查看:78
本文介绍了PHP readdir()不按字母顺序返回文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览包含一些图片的目录,并且使用readdir()的非常简单的实现,例如:

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

if ($handle = opendir($path)) {
    while (false !== ($szFilename = readdir($handle))) {
    if ($szFilename[0] !== '.') {
        if (is_file($path.$szFilename)) {
                // do stuff
            }
        }
     }
 }

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

The problem that I am having is that the files are not being read in alphabetical order as the docs for readdir() state:

返回下一个文件的文件名 从目录中.文件名是 按他们返回的顺序 由文件系统存储.

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堆栈的服务器上运行.

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.

我知道我可以构建一个数组并对其进行排序,但是我想知道我在做的事情中是否缺少某些东西.

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.

推荐答案

字母顺序::我认为您误读了引述的代码段...

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.

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

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.

您可能要考虑使用 scandir 作为您的基础如果必须按字母顺序排序,则需要付出很大的努力. :)

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

这篇关于PHP readdir()不按字母顺序返回文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆