dongshuzui0335 2010-12-01 21: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

  1. Aardvark.txt
  2. BarDir
  3. BazDir
  4. Dante.pdf
  5. FooDir

and I want

  1. BarDir
  2. BazDir
  3. FooDir
  4. Aardvark.txt
  5. 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-01 21: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部