【问题标题】:Using php to create a Folder select list - including child folders?使用 php 创建文件夹选择列表 - 包括子文件夹?
【发布时间】:2010-06-23 01:36:58
【问题描述】:

我正在尝试使用 scandir 显示特定目录中列出的文件夹的选择列表(效果很好)但是,我还需要它来将子文件夹(如果有的话)添加到我的选择列表中。如果有人可以帮助我,那就太好了!

这是我想要的结构:

<option>folder 1</option>
<option> --child 1</option>
<option> folder 2</option>
<option> folder 3</option>
<option> --child 1</option>
<option> --child 2</option>
<option> --child 3</option>

这是我从该线程 (Using scandir() to find folders in a directory (PHP)) 获得的代码(仅显示父文件夹):

 $dir = $_SERVER['DOCUMENT_ROOT']."\\folder\\";

 $path = $dir;
 $results = scandir($path);

 $folders = array();
 foreach ($results as $result) {
    if ($result == '.' || $result == '..') continue;
    if (is_dir($path . '/' . $result)) {
      $folders[] = $result;
    };
 };

^^ 但我也需要它来显示子目录。如果有人可以提供帮助,那就太好了! :)

编辑:忘了说我不想要文件,只想要文件夹..

【问题讨论】:

    标签: php select directory option children


    【解决方案1】:
    //Requires PHP 5.3
    $it = new RecursiveTreeIterator(
        new RecursiveDirectoryIterator($dir));
    
    foreach ($it as $k => $v) {
        echo "<option>".htmlspecialchars($v)."</option>\n";
    }
    

    您可以自定义前缀为RecursiveTreeIterator::setPrefixPart

    【讨论】:

      【解决方案2】:
      /* FUNCTION: showDir
       * DESCRIPTION: Creates a list options from all files, folders, and recursivly
       *     found files and subfolders. Echos all the options as they are retrieved
       * EXAMPLE: showDir(".") */
      function showDir( $dir , $subdir = 0 ) {
          if ( !is_dir( $dir ) ) { return false; }
      
          $scan = scandir( $dir );
      
          foreach( $scan as $key => $val ) {
              if ( $val[0] == "." ) { continue; }
      
              if ( is_dir( $dir . "/" . $val ) ) {
                  echo "<option>" . str_repeat( "--", $subdir ) . $val . "</option>\n";
      
                  if ( $val[0] !="." ) {
                      showDir( $dir . "/" . $val , $subdir + 1 );
                  }
              }
          }
      
          return true;
      }
      

      【讨论】:

      • 谢谢你,但它也显示文件 - 我只想要文件夹本身:)
      • 啊,我为你修好了 :) 如果你需要它来显示 .和..,在 $scan = scandir 之后添加以下行: if ( $subdir == 0 ) { echo " "; }
      • 等待弄清楚了,不得不将我的 $dir 添加到 is_dir($val) 等 :D 谢谢你的帮助! :)
      • 大声笑,很好。我刚刚用完全相同的更改对其进行了更新。没问题,很高兴帮助您入门! :)
      【解决方案3】:

      您可以使用 PHP "glob" 函数 http://php.net/manual/en/function.glob.php ,并构建一个递归函数(一个调用自身的函数)以达到无限层次的深度。它比使用“scandir”更短

      function glob_dir_recursive($dirs, $depth=0) {
          foreach ($dirs as $item) {
              echo '<option>' . str_repeat('-',$depth*1) . basename($item) . '</option>'; //can use also "basename($item)" or "realpath($item)"
              $subdir =  glob($item . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR); //use DIRECTORY_SEPARATOR to be OS independent
              if (!empty($subdir)) { //if subdir array is not empty make function recursive
                  glob_dir_recursive($subdir, $depth+1); //execute the function again with current subdir, increment depth
              }
          }
      }
      

      用法:

      $dirs = array('galleries'); //relative path examples: 'galleries' or '../galleries' or 'galleries/subfolder'.
      //$dirs = array($_SERVER['DOCUMENT_ROOT'].'/galleries'); //absolute path example
      //$dirs = array('galleries', $_SERVER['DOCUMENT_ROOT'].'/logs'); //multiple paths example
      
      echo '<select>';
      glob_dir_recursive($dirs); //to list directories and files
      echo '</select>';
      

      这将准确生成请求的输出类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-26
        • 2011-12-25
        • 1970-01-01
        • 2015-06-07
        • 1970-01-01
        • 2012-08-08
        相关资源
        最近更新 更多