PHP - 在下拉菜单中递归列出所有目录和子目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14304935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:02:47  来源:igfitidea点击:

PHP - Listing all directories and sub-directories recursively in drop down menu

phpdrop-down-menurecursiondirectorysubdirectory

提问by Brian

Possible Duplicate:
PHP Get all subdirectories of a given directory

可能的重复:
PHP 获取给定目录的所有子目录

I want a drop down menu to show all sub-directories in ./files/$userid/not just the main folder. For example: /files/$userid/folder1/folder2/

我想要一个下拉菜单来显示所有子目录,./files/$userid/而不仅仅是主文件夹。例如: /files/$userid/folder1/folder2/

My current code is:

我目前的代码是:

HTML:

HTML:

<select name="myDirs">
<option value=""  selected="selected">Select a folder</option>

PHP:

PHP:

if (chdir("./files/" . $userid)) {

       $dirs = glob('*', GLOB_ONLYDIR);
       foreach($dirs as $val){
          echo '<option value="'.$val.'">'.$val."</option>\n";
       }        
        } else {
       echo 'Changing directory failed.';
}

回答by PleaseStand

RecursiveDirectoryIteratorshould do the trick. Unfortunately, the documentation is not great, so here is an example:

RecursiveDirectoryIterator应该可以解决问题。不幸的是,文档不是很好,所以这里有一个例子:

$root = '/etc';

$iter = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($root, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST,
    RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);

$paths = array($root);
foreach ($iter as $path => $dir) {
    if ($dir->isDir()) {
        $paths[] = $path;
    }
}

print_r($paths);

This generates the following output on my computer:

这会在我的计算机上生成以下输出:

Array
(
    [0] => /etc
    [1] => /etc/rc2.d
    [2] => /etc/luarocks
    ...
    [17] => /etc/php5
    [18] => /etc/php5/apache2
    [19] => /etc/php5/apache2/conf.d
    [20] => /etc/php5/mods-available
    [21] => /etc/php5/conf.d
    [22] => /etc/php5/cli
    [23] => /etc/php5/cli/conf.d
    [24] => /etc/rc4.d
    [25] => /etc/minicom
    [26] => /etc/ufw
    [27] => /etc/ufw/applications.d
    ...
    [391] => /etc/firefox
    [392] => /etc/firefox/pref
    [393] => /etc/cron.d
)

回答by unused

You can write your own recursive listing of the directories like:

您可以编写自己的目录递归列表,例如:

function expandDirectories($base_dir) {
      $directories = array();
      foreach(scandir($base_dir) as $file) {
            if($file == '.' || $file == '..') continue;
            $dir = $base_dir.DIRECTORY_SEPARATOR.$file;
            if(is_dir($dir)) {
                $directories []= $dir;
                $directories = array_merge($directories, expandDirectories($dir));
            }
      }
      return $directories;
}

$directories = expandDirectories(dirname(__FILE__));
print_r($directories);

回答by Alex

You can use a recursive glob implementation like in this function:

您可以在此函数中使用递归 glob 实现:

function rglob($pattern='*', $path='', $flags = 0) {
$paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
$files=glob($path.$pattern, $flags);
foreach ($paths as $path) {
  $files=array_merge($files,rglob($pattern, $path, $flags));
}
return $files;
}