The traversal results are as follows:
Program code
The jquery file in index. php can be downloaded by Baidu, because this is used to achieve an effect.
The code is as follows: |
Copy code |
<Script src = "jquery/jquery-1.3.1.js" type = "text/javascript"> </script> <Style type = "text/css"> Body {Font: normal 12px arial, tahoma, helvetica, sans-serif; margin: 0; background: # fff; padding: 30px ;} * {Margin: 0; padding: 0 ;} Ul {visibility: visible; cursor: pointer ;} . SimpleTree li {font-size: 14px; list-style: none; margin: 0 0 0 50px; padding: 0 0 0 34px; line-height: 18px; margin-left: -13px; background: url (jquery/images/expandable.gif) 0-2px no-repeat # fff ;} . SimpleTree li span {display: inline; clear: left; white-space: nowrap ;} Li. root {padding: 0 0 0 20px; line-height: 20px; background: url (jquery/images/root.gif) 0 2px no-repeat # fff ;} Li. file {padding: 0 0 0 35px; line-height: 20px; background: url (jquery/images/leaf-last.gif) 0 2px no-repeat # fff ;} </Style> <Script type = "text/javascript"> $ (Function (){ $ (". SimpleTree"). children ("li"). find ("ul"). hide (); $ ("Span"). click (function (){ Var $ this_ul = $ (this). siblings ("ul "); If ($ this_ul.is (": visible ")){ $ This_ul.stop (false, true). hide (); } Else { $ (This ). siblings ("ul "). stop (false, true ). show (). end (). stop (false, true ). siblings ("ul "). find ("ul "). hide (); } }) })
</Script> <? Php Include ("function. php "); $ Path = "directory/"; // directory name Echo "<ul class = 'simpletree'> <li class = 'root'> <span> directory </span>"; // directory name, which is the same as the name in path List_dir ($ path ); Echo "</ul> </li> "; ?> |
Function. php: This file contains the function used to traverse the directory.
The code is as follows: |
Copy code |
<? Php /* Output the number of all files in the current directory */ Function files_count ($ path, & $ I = 0 ){ If ($ opendir = opendir ($ path )){ // ======================== While ($ file = readdir ($ opendir )){ If ($ file! = "." & $ File! = ".."){ If (is_file ($ path. "/". $ file )){ $ I ++; ; } Else { Files_count ($ path. "/". $ file, $ I ); } } } // ==================== Return "(". $ I .")"; } } // Echo files_count ("directory/directory 1/3 /"); // ===================================== // /* Traverse the directory */ Function list_dir ($ path ){ If ($ opendir = opendir ($ path )){ } Echo "<ul> "; While ($ file = readdir ($ opendir )){ If ($ file! = "." & $ File! = ".."){
If (is_dir ($ path. "/". $ file )){ $ Bar = scandir ($ path. "/". $ file ); Unset ($ bar [0]); Unset ($ bar [1]); If (count ($ bar! = 0 )){ Echo "<li> <span>". $ file. files_count ($ path. "/". $ file). "</span> "; List_dir ($ path. "/". $ file ); } } Else {
Echo "<li class = 'file'> <a href = '". $ path. "/". $ file. "'> ". $ file. "</a> </li> "; } } } Echo "</ul> </li> "; } ?> |