php中的文件夹树

时间:2013-08-28 15:49:12

标签: 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('.'); ?>

2 个答案:

答案 0 :(得分:5)

将最大递归级别传递给函数。通过这种方式,您可以在运行时确定要达到的深度级别。

此外,(我认为)将“我希望dirs或者不是”决策外部完成并作为参数传递将是一个好主意。这样一个功能就可以做到这两点。

最后,拥有一个函数输出HTML并不是一个好主意。最好将其作为字符串返回,以便您可以更自由地移动代码。理想情况下,您希望将所有逻辑与您的演示文稿视图分开(并且不止于此;谷歌'MVC')。

更好的方法是将HTML 模板传递给mkmap函数,并让它使用它来创建HTML代码段。这样,如果在一个地方你想要一个<ul>而在另一个地方想要一个<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.

模板

有很多模板化函数的方法,但也许最简单的是(对于更精细的自定义,XML是必需的 - 使用字符串函数管理HTML有nasty space-time continuum implications):

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];
}

该函数的工作方式与之前类似,但您可以传递另一个参数来自定义它:

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

答案 1 :(得分:1)

要检查文件是否为文件夹,请使用is_dir()功能。

此递归解决方案将列出文件夹和子文件夹:

 <?php

function mkmap($dir){
    $ffs = scandir($dir);
    echo '<ul>';
    foreach($ffs as $file){
        if($file != '.' && $file!= '..' ){

            $path=$dir.'/'.$file;
             echo "<li><a href='".$path."'>$file</a></li>";           

            if(is_dir($dir.'/'.$file)) mkmap($dir.'/'.$file);

        }
    }
    echo '</ul>';
}

mkmap('main dir');
?>
相关问题