生成目录树形结构

<?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);

?>

 

你可能感兴趣的:(生成)