在目录中获取文件夹,然后获取其子文件夹 PHP代码: 问题:

在目录中获取文件夹,然后获取其子文件夹 PHP代码: 问题:,php,Php,“我的while循环”中的“我的while循环”未显示子文件夹 结果如何: 这看起来完全是一团糟,我知道,但如果你不去尝试,你将一事无成!我以前的工作版本只得到了一级文件夹 <?php

“我的while循环”中的“我的while循环”未显示子文件夹

结果如何:
这看起来完全是一团糟,我知道,但如果你不去尝试,你将一事无成!我以前的工作版本只得到了一级文件夹

<?php
    echo '<ul class="DirView">';
    $path = "../Desktop/";
    $dir = new DirectoryIterator($path);
    foreach ($dir as $fileinfo) {
        if ($fileinfo->isDir() && !$fileinfo->isDot()) {
            echo '<li>'.$fileinfo->getFilename().'</li>';
        }
    }
    echo '</ul>';
?>

不确定我是否完全同意这个答案,但您可能在内部foreach循环中误用了变量:

您当前使用的是
$fileinfo
而不是
$subPath
,请尝试:

foreach ($subDir as $subPath) {
    if ($subPath->isDir() && !$subPath->isDot()) {
        echo '<li><a href="#">'.$subPath->getFilename().'<span>18</span></a><li>';
    }
}
foreach($subDir作为$subPath){
如果($subPath->isDir()&&!$subPath->isDot()){
回音“
  • ”; } }
  • 完整代码

    <?php
        echo '<ul class="DirView"><li><a href="#">Recently Used<span>28</span></a></li>';
        $path = "../Desktop/";
        $dir = new DirectoryIterator($path);
        foreach ($dir as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                $subPath = $path.$fileinfo->getFilename();
                $subDir = new DirectoryIterator($subPath);
                echo '<li><a href="#">'.$fileinfo->getFilename().'<span>28</span></a><ul>';
                foreach ($subDir as $subPath) {
                    if ($subPath->isDir() && !$subPath->isDot()) {
                        echo '<li><a href="#">'.$subPath->getFilename().'<span>18</span></a><li>';
                    }
                }
                echo '</ul></li>';
            }
        }
        echo '</ul>';
    ?>
    
    
    
    foreach ($subDir as $subPath) {
        if ($subPath->isDir() && !$subPath->isDot()) {
            echo '<li><a href="#">'.$subPath->getFilename().'<span>18</span></a><li>';
        }
    }
    
    <?php
        echo '<ul class="DirView"><li><a href="#">Recently Used<span>28</span></a></li>';
        $path = "../Desktop/";
        $dir = new DirectoryIterator($path);
        foreach ($dir as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                $subPath = $path.$fileinfo->getFilename();
                $subDir = new DirectoryIterator($subPath);
                echo '<li><a href="#">'.$fileinfo->getFilename().'<span>28</span></a><ul>';
                foreach ($subDir as $subPath) {
                    if ($subPath->isDir() && !$subPath->isDot()) {
                        echo '<li><a href="#">'.$subPath->getFilename().'<span>18</span></a><li>';
                    }
                }
                echo '</ul></li>';
            }
        }
        echo '</ul>';
    ?>