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


How to list all files in folders and sub-folders using scandir and display them as an option in select?

我有一段代码,我想修改以列出当前路径中文件夹和子文件夹中的所有文件。我已经找到了几种可能的解决办法。我试图实现他们实际上,但似乎没有工作,所以我不确定他们是否真的工作或不,或者只是我实现了他们的错误。这是我当前的代码:

<?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); 
?>

当前代码显示结果:

$currentdir
   Option1
   Option2
   Option3
   SUB-FOLDER1
   SUB-FOLDER2

有人可以帮助我,告诉我如何编写/重写代码使用现有的一个显示文件夹和其他子文件夹中的文件,看起来像这样:

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

我真的非常需要一个解决方案,提前感谢大家。

虽然其他答案都很好,也很正确,但请允许我添加我的解决方案:

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

使用递归调用来获取子项。注意,为了更好地模拟文件结构,我在每个项目之前添加了一些空白。此外,我关闭了optgroup立即,以防止结束嵌套的optgroup元素,这是无效的HTML。

查找指定目录下的所有文件和文件夹

function scanDirAndSubdir($dir, &$out = []) {
    $sun = scandir($dir);
    foreach ($sun as $a => $filename) {
        $way = realpath($dir . DIRECTORY_SEPARATOR . $filename);
        if (!is_dir($way)) {
            $out[] = $way;
        } else if ($filename != "." && $filename != "..") {
            scanDirAndSubdir($way, $out);
            $out[] = $way;
        }
    }
    return $out;
}
var_dump(scanDirAndSubdir('C:/xampp/htdocs/test/'));
示例:

array (size=4)
  0 => string 'C:/xampp/htdocs/test/text1.txt' (length=30)
  1 => string 'C:/xampp/htdocs/test/text1.txt' (length=30)
  2 => string 'C:/xampp/htdocs/test/subfolder1/text8.txt' (length=41)
  3 => string 'C:/xampp/htdocs/test/subfolder4/text9.txt' (length=41)

使用DirectoryIterator查找文件夹这是我的方法

$di = new DirectoryIterator("/dir");
foreach($di as $dir){
    if ($dir->isDot()) continue;
    echo "<option value='", $dir->getFilename(), "'>", $dir->getFilename(), "</option>";
}

用于查找子目录

$ri = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach($ri as $dir){
    if ($dir->isDot()) continue;
    echo "<option value='", $dir->getFilename(), "'>", $dir->getFilename(), "</option>";
}

如果你不想使用上面的语句,那么这是另一个选择:

    <?php
function listdir($currentdir){
    $dir = opendir($currentdir);
    $file = readdir($dir);
    echo "<optgroup label='$currentdir'>";
    do {
        if (is_dir($currentdir."/".$file) && $file != "." && $file != ".."){
            listdir($currentdir."/".$file);
            echo $currentdir."/".$file;
        } else if($file != "." && $file != "..") {
            echo "<option value='$file'>$file</option>";
        } else {
            continue;
        }
    } while($file = readdir($dir));
    echo "</optgroup>";
    closedir($dir); 
}
echo '<select name="workout">';
listdir('/var/www'); //change to your directory
echo '</select>';
?>