doushifang4382 2010-06-23 01:36
浏览 73
已采纳

使用php创建文件夹选择列表 - 包括子文件夹?

I'm trying to use scandir to display a select list of folders listed in a specific directory (which works fine) however, I need it to also add the child folders (if there are any) into my select list. If anyone could help me, that would be great!

This is the structure I want:

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

And this is the code I have (which only shows the parent folders) which I got from this thread (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;
    };
 };

^^ but I need it to show the child directories also.. If anyone could help, that'd be great! :)

EDIT: Forgot to say that I don't want the files, only the folders..

  • 写回答

3条回答 默认 最新

  • dream890110 2010-06-23 02:15
    关注
    /* 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>
    ";
    
                if ( $val[0] !="." ) {
                    showDir( $dir . "/" . $val , $subdir + 1 );
                }
            }
        }
    
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流
  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型