【问题标题】:How to get glob() to return the same files order as opendir()?如何让 glob() 返回与 opendir() 相同的文件顺序?
【发布时间】:2012-07-19 11:49:09
【问题描述】:

我希望glob 返回与opendir 相同的文件顺序(排序)

例如:

$files = glob('/home/web/public_html/audio/*.mp3');
foreach($files as $file) {
 echo $file;
 break;
}

if ($handle = opendir('/home/web/public_html/audio/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            echo "$entry\n";
            break;
        }
    }
    closedir($handle);
}
?>

有没有办法做到这一点?

【问题讨论】:

    标签: php opendir


    【解决方案1】:

    您可以使用GLOB_NOSORT 标志,这将使glob() 返回目录中出现的文件。

    readdir 不对文件进行排序,因此GLOB_NOSORT 会复制此行为。来自文档:

    条目按文件系统存储的顺序返回。

    【讨论】:

    • 你能发布他们两个的输出吗?你在他们的排序中发现了什么规律吗?
    猜你喜欢
    • 2020-06-07
    • 2013-05-26
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2021-03-20
    • 2010-12-04
    • 1970-01-01
    • 2020-01-03
    相关资源
    最近更新 更多