加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php+jquery实现无限级目录遍历展现代码

发布时间:2023-06-06 13:50:49 所属栏目:PHP教程 来源:
导读:这个例子可以利用php目录遍历出来的目录或文件进行一个树型的展示效果,程序代码,index.php 里面的jquery文件大家可百度下载一个,因为这是用来实现一个效果的:

index.php:

<script src="jquery/jquery-1.3.1.js
这个例子可以利用php目录遍历出来的目录或文件进行一个树型的展示效果,程序代码,index.php 里面的jquery文件大家可百度下载一个,因为这是用来实现一个效果的:

index.php:

<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="目录/";//目录名 
echo "<ul class='simpleTree'><li class='root'><span>目录</span>"; //目录名,和path 中名称一样 
list_dir($path); 
echo "</ul></li>"; 
?> 
function.php 这个文件包含了遍历目录的函数了,代码如下:

<?php 
/*输出当前目录下的所有文件数量*/ 
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("目录/目录1/3/"); 
//=============================// 
/*遍历目录*/ 
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>"; 

?> 
 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章