gpt4 book ai didi

PHP scandir 结果 : sort by folder-file, 然后按字母顺序排列

转载 作者:可可西里 更新时间:2023-10-31 22:16:05 24 4
gpt4 key购买 nike

scandir 的 PHP 手册: 默认情况下,排序顺序为字母升序

我正在构建一个文件浏览器(在 Windows 中),所以我希望返回的地址按文件夹/文件排序,然后在这些子集中按字母顺序排列。

例子:现在,我扫描并输出

Aardvark.txt
BarDir
BazDir
Dante.pdf
FooDir

我想要

BarDir
BazDir
FooDir
Aardvark.txt
Dante.pdf

除了 usortis_dir() 解决方案(我自己可以弄清楚),有没有一种快速有效的方法做这个?

this comment的忍者走在正确的轨道上 - 这是最好的方法吗?

最佳答案

这给了你想要的吗?

function readDir($path) {

// Make sure we have a trailing slash and asterix
$path = rtrim($path, '/') . '/*';

$dirs = glob($path, GLOB_ONLYDIR);

$files = glob($path);

return array_unique(array_merge($dirs, $files));

}

$path = '/path/to/dir/';

readDir($path);

请注意,您不能对文件glob('*.*'),因为它会选取名为like.this 的文件夹。

关于PHP scandir 结果 : sort by folder-file, 然后按字母顺序排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4331853/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com