Php + jquery implements infinite directory traversal and display code

Source: Internet
Author: User

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> ";
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.