duanjizhan9353 2014-07-31 19:20
浏览 76
已采纳

如何使用scandir列出文件夹和子文件夹中的所有文件,并将它们显示为select中的选项?

I have a piece of code that I would like to modify to list all files in folders and sub-folders in the current path. I have found several possible solutions. I tried to implement them actually, but nothing seemed to work so I am not sure if they are actually working or not or it was just me implemented them wrong. Either way this is my current code:

<?php 
        $currentdir = 'C:/xampp/htdocs/test/'; //change to your directory
        $dir = opendir($currentdir);
        echo '<select name="workout">';
            $file = preg_grep('/^([^.])/', scandir($dir));
            while($file = readdir($dir))
            {
                if ($file != "." && $file != "..") {
                    echo "<option value='$file'>$file</option>";
            }           
                else {
                    continue;
            }
        }
        echo '</select>';
        closedir($dir); 
?>

Currently this code shows results:

$currentdir
   Option1
   Option2
   Option3
   SUB-FOLDER1
   SUB-FOLDER2

Can someone help me and show me how to write/rewrite the code using existing one to display files from folders and other sub-folders to look something like this:

$currentdir
  Option1
  Option2
  Option3
    SUB-FOLDER1
      Option1
      Option2
      Option3
    SUB-FOLDER2
      Option1
      Option2
      Option3

I became really desperate for a solution and thank you all in advance.

  • 写回答

4条回答 默认 最新

  • duanbei1598 2014-07-31 20:57
    关注

    While the other answers are fine and correct, allow me to add my solution as well:

    function dirToOptions($path = __DIR__, $level = 0) {
        $items = scandir($path);
        foreach($items as $item) {
            // ignore items strating with a dot (= hidden or nav)
            if (strpos($item, '.') === 0) {
                continue;
            }
    
            $fullPath = $path . DIRECTORY_SEPARATOR . $item;
            // add some whitespace to better mimic the file structure
            $item = str_repeat('&nbsp;', $level * 3) . $item;
            // file
            if (is_file($fullPath)) {
                echo "<option>$item</option>";
            }
            // dir
            else if (is_dir($fullPath)) {
                // immediatly close the optgroup to prevent (invalid) nested optgroups
                echo "<optgroup label='$item'></optgroup>";
                // recursive call to self to add the subitems
                dirToOptions($fullPath, $level + 1);
            }
        }
    
    }
    
    echo '<select>';
    dirToOptions();
    echo '</select>';
    

    It uses a recursive call to fetch the subitems. Note that I added some whitespace before each item to better mimic the file structure. Also I closed the optgroup immediatly, to prevent ending up with nested optgroup elements, which is invalid HTML.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化