PHP中的文件夹树

Gil*_*OOl 1 php readdir opendir

我想在php中列出文件夹的内容。

但是我只想显示文件夹而不是文件。

另外,我想最多显示一个子文件夹!

请问你能帮帮我吗 !

我的代码:

<?php
function mkmap($dir){
    echo "<ul>";   
    $folder = opendir ($dir);

    while ($file = readdir ($folder)) {   
        if ($file != "." && $file != "..") {           
            $pathfile = $dir.'/'.$file;           
            echo "<li><a href=$pathfile>$file</a></li>";           
            if(filetype($pathfile) == 'dir'){               
                mkmap($pathfile);               
            }           
        }       
    }
    closedir ($folder);    
    echo "</ul>";   
}
?>

<?php mkmap('.'); ?>
Run Code Online (Sandbox Code Playgroud)

LSe*_*rni 5

将最大递归级别传递给该函数。这样,您可以在运行时确定要深入多少个级别。

另外,最好在外部进行“我是否想要dirs”决定并将其作为参数传递(这是一个好主意)。这样,一个功能可以同时完成这两项工作。

最后,让函数输出HTML很少是一个好主意。最好以字符串形式返回它,这样您就可以自由移动代码了。理想情况下,您希望将所有逻辑与演示文稿视图(以及更多的谷歌“ MVC”)分开。

更好的方法是将HTML 模板传递给该mkmap函数,并使用它来创建HTML代码段。这样,如果在一个地方要a <ul>,在另一个地方要a <ul id="another-tree" class="fancy">,则不必使用同一功能的两个版本;但这可能是过大的(str_replace不过,如果需要,您可以使用或XML函数轻松完成此操作)。

function mkmap($dir, $depth = 1, $only_dirs = True){
    $response = '<ul>';
    $folder = opendir ($dir);

    while ($file = readdir ($folder)) {
        if ($file != '.' && $file != '..') {           
            $pathfile = $dir.'/'.$file;
            if ($only_dirs && !is_dir($pathfile))
                continue;
            $response .= "<li><a href=\"$pathfile\">$file</a></li>";
            if (is_dir($pathfile) && ($depth !== 0))
                $response .= mkmap($file, $depth - 1, $only_dirs);
        }
    }
    closedir ($folder);
    $response .= '</ul>';
    return $response;
}


// Reach depth 5
echo mkmap('Main Dir', 5, True);

// The explicit check for depth to be different from zero means
// that if you start with a depth of -1, it will behave as "infinite depth",
// which might be desirable in some use cases.
Run Code Online (Sandbox Code Playgroud)

模板化

对该函数进行模板化的方法有很多,但也许是最简单的(对于更复杂的自定义,XML是强制性的-使用字符串函数管理HTML具有讨厌的时空连续性含义):

function mkmap($dir, $depth = 1, $only_dirs = True,
    $template = False) {
    if (False === $template) {
        $template = array('<ul>','<li><a href="{path}">{file}</a></li>','</ul>');
    }
    $response = '';
    $folder = opendir ($dir);

    while ($file = readdir ($folder)) {
        if ($file != '.' && $file != '..') {           
            $pathfile = $dir.'/'.$file;
            if ($only_dirs && !is_dir($pathfile))
                continue;
            $response .= str_replace(array('{path}','{file}'), array($pathfile, $file), $template[1]);
            if (is_dir($pathfile) && ($depth !== 0))
                $response .= mkmap($file, $depth - 1, $only_dirs, $template);
        }
    }
    closedir ($folder);
    return $template[0] . $response . $template[2];
}
Run Code Online (Sandbox Code Playgroud)

该函数的工作方式与以前一样,但是您可以传递另一个自定义参数:

echo mkmap('Main Dir', 5, True, array(
    '<ul class="filetree">',
    '<li><a href="{path}"><img src="file.png" /><tt>{file}</tt></a></li>',
    '</ul>'));
Run Code Online (Sandbox Code Playgroud)