码迷,mamicode.com
首页 > 其他好文 > 详细

生成目录树形结构

时间:2015-04-13 12:38:33      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

<?php
echo ‘<script src="http://code.jquery.com/jquery.min.js"></script>‘;
echo ‘<script>$(function(){$(".expandable").click(function(){event.preventDefault();event.stopPropagation();$(this).children().toggle();});$(".file").click(function(){event.stopPropagation();event.preventDefault();});});</script>‘;
echo ‘<style>.expandable{cursor:pointer;}.file{color:purple;}</style>‘;
function traverse($path, $x, $y) {
    $current_dir = opendir($path);
    while (($file = readdir($current_dir)) !== false) {
        $sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径
        if ($file == ‘.‘ || $file == ‘..‘) {
            continue;
        } else if (is_dir($sub_dir)) {
            for ($i = 0; $i < $x; $i++) {
                echo "│";
            }
            echo "│┼" . $file . "<br>";
            traverse($sub_dir, $x + 1, $y + 1);
        } else {
            for ($i = 0; $i < $x; $i++) {
                echo "│";
            }
            echo "│├" . $file . "<br>";
        }
    }
}
$countFile = 0;
function traverse2($path, $x, $y) {
    global $countFile;
    $current_dir = opendir($path);
    while (($file = readdir($current_dir)) !== false) {
        $sub_dir = $path . DIRECTORY_SEPARATOR . $file; //构建子目录路径
        if ($file == ‘.‘ || $file == ‘..‘) {
            continue;
        } else if (is_dir($sub_dir)) {
            echo "<li class=‘expandable level" . $x . "‘>" . $file . "<ul>";
            traverse2($sub_dir, $x + 1, $y + 1);
            echo "</ul></li>";
        } else {
            if (substr($file, -3) == ‘php‘) {
                $countFile++;
                echo "<li class=‘file‘>" . $file . "</li>";
            }

        }
    }
}
echo "<div id=‘control‘><button onclick=‘$(\".expandable\").children().hide();‘>全部折叠</button><button onclick=‘$(\".expandable\").children().show();‘>全部展开</button></div>";

echo "<ul style=‘position:relative;‘>";
traverse2(‘.‘, 0, 0);
echo "</ul>";
echo "<p style=‘clear:both;position:relative‘>共计" . $countFile . "个文件</p>";
// traverse(".", 0, 0);
?>

 

生成目录树形结构

标签:

原文地址:http://www.cnblogs.com/sky-view/p/4421735.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!