首页 > 解决方案 > PHP目录/文件树结构 - 排序结果

问题描述

我有创建文件夹、子文件夹和文件的树结构的脚本。一切都很完美,但结果是未排序的。

我想我需要将第一个循环添加到一个数组中,然后对其进行排序。但是我的每一次实验都失败了......

请问,问题怎么解决?

非常感谢。

    function createDir($path = '/home/www/mydomain/www/') {
// I think I need an array here 
      if ($handle = opendir($path)) {
        echo "<ul>";
        $queue = array();
        while (false !== ($file = readdir($handle))) {
          if (is_dir($path.$file) && $file != '.' && $file !='..') { 
            printSubDir($file, $path, $queue);
            }
          else if ($file != '.' && $file !='..') {
            $queue[] = $file;
            }
          }
        printQueue($queue, $path);
            echo "</ul>";
        }
      }
    
    function printQueue($queue, $path) {
      asort($queue);  // I tried this - it sorts only files and sub-folders but no root-folders
      foreach ($queue as $file) {
        printFile($file, $path);
        }
      }
    
    function printFile($file, $path) {
      echo "<li><a href=\"".$path.$file."\">$file</a></li>";
      }
    
    function printSubDir($dir, $path) {
      echo "<li><span class=\"toggle\">$dir</span>";
      createDir($path.$dir."/");
      echo "</li>";
      }
    
    createDir();

标签: phparrayssorting

解决方案


推荐阅读