您的位置:首页 > 其它

生成目录树形结构

2015-04-13 11:32 281 查看
<?php
echo '<script src="http://code.jquery.com/jquery.min.js"></script>';
echo '<script>
function showpath(str,t){clearTimeout(t);$("#file-path").remove();$("<div id=\'file-path\'></div>").appendTo("body").html(str);t=setTimeout("$(\'#file-path\').fadeOut()",10000);return t;}

$(function(){
var t={};
$(".expandable").click(function(){event.preventDefault();event.stopPropagation();$(this).children().toggle();});
$(".file").click(function(){event.stopPropagation();event.preventDefault();});
$(".file").click(function(){var tmpstr="";clearTimeout(t);
$(this).parents("li").each(function(){tmpstr=($(this)[0].firstChild.nodeValue+"→"+tmpstr);});
tmpstr+=$(this).text();t=showpath(tmpstr,t);
});$("body").on("click","#file-path",function(){clearTimeout(t);$(this).fadeOut();});
});</script>';
echo '<style>.expandable{cursor:pointer;font-weight:bold}.file{color:blue;cursor:pointer;}#file-path{font-size:20px;font-weight:bold;position:fixed;width:98%;cursor:pointer;padding:10px 15px;text-align:center;top:0px;box-sizing:border-box;left:1%;border:2px solid black;background:rgba(0,188,188,0.6);border-radius:5px;}</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 "<li class='file'>" . $file . "</li>";

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

echo "<ol style='position:relative;'>";
traverse2('.', 0, 0);
echo "</ol>";
echo "<p style='clear:both;position:relative'>共计" . $countFile . "个文件</p>";
// traverse(".", 0, 0);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: