在PHP中按字母顺序从文件夹递归获取文件夹和文件? [英] Get folders and files recursively from a folder in alphabetical order in PHP?

查看:157
本文介绍了在PHP中按字母顺序从文件夹递归获取文件夹和文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按字母顺序从文件夹中递归所有文件夹和文件(文件夹首先是文件)

I need to get all the folders and files from a folder recursively in alphabetical order (folders first, files after)

是否有实现的PHP功能,这个?

Is there an implemented PHP function which caters for this?

我有这个功能:

function dir_tree($dir) {
   $path = '';
   $stack[] = $dir;
   while ($stack) {
       $thisdir = array_pop($stack);
       if ($dircont = scandir($thisdir)) {
           $i=0;
           while (isset($dircont[$i])) {
               if ($dircont[$i] !== '.' && $dircont[$i] !== '..' && $dircont[$i] !== '.svn') {
                   $current_file = "{$thisdir}/{$dircont[$i]}";
                   if (is_file($current_file)) {
                       $path[] = "{$thisdir}/{$dircont[$i]}";
                   } elseif (is_dir($current_file)) {
                        $path[] = "{$thisdir}/{$dircont[$i]}";
                       $stack[] = $current_file;
                   }
               }
               $i++;
           }
       }
   }
   return $path;
}

我已经排序了数组并打印出来:

I have sorted the array and printed it like so:

$filesArray = dir_tree("myDir");
natsort($filesArray);

foreach ($filesArray as $file) {
    echo "$file<br/>";
}

我需要知道什么时候找到一个新的子目录,所以我可以添加一些空格,将其打印在像结构这样的目录中而不是列表中。

What I need is to know when a new sub directory is found, so I can add some spaces to print it in a directory like structure instead of just a list.

任何帮助?

非常感谢

推荐答案

我发现一个链接,帮助我很多,我试图实现:

I found a link which helped me a lot in what I was trying to achieve:

http://snippets.dzone.com/posts/show / 1917

这可能有助于别人,它首先创建一个包含文件夹的文件,之后是文件。当您单击子文件夹时,它将提交,并生成另一个页面,其中包含分配文件夹中的文件夹和文件。

This might help someone else, it creates a list with folders first, files after. When you click on a subfolder, it submits and another page with the folders and files in the partent folder is generated.

这篇关于在PHP中按字母顺序从文件夹递归获取文件夹和文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆