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 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: