dongshuzui0335 2010-12-02 05:01
浏览 664
已采纳

PHP scandir结果:按文件夹文件排序,然后按字母顺序排序

PHP manual for scandir: By default, the sorted order is alphabetical in ascending order.

I'm building a file browser (in Windows), so I want the addresses to be returned sorted by folder/file, then alphabetically in those subsets.

Example: Right now, I scan and output

Aardvark.txt
BarDir
BazDir
Dante.pdf
FooDir

and I want

BarDir
BazDir
FooDir
Aardvark.txt
Dante.pdf

Other than a usort and is_dir() solution (which I can figure out myself), is there a quick and efficient way to do this?

The ninja who wrote this comment is on the right track - is that the best way?

  • 写回答

3条回答 默认 最新

  • dpf5207 2010-12-02 05:12
    关注

    Does this give you what you want?

    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);
    

    Note that you can't glob('*.*') for files because it picks up folders named like.this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab