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

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

报告相同问题?

悬赏问题

  • ¥20 蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏