最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

PHP实现递归循环每一个目录程序

时间:2014-06-09 16:40来源: 作者: 点击: 次
要遍历一个文件夹里面的所有目录,列出里面所有的文件,PHP本身自带的有一个readdir的函数,不过只能读取当前的目录,根据这个函数,我写了另外一个函数,用来实现我的需求,代码如下: ?php class l

要遍历一个文件夹里面的所有目录,列出里面所有的文件,PHP本身自带的有一个readdir的函数,不过只能读取当前的目录,根据这个函数,我写了另外一个函数,用来实现我的需求,代码如下:

  1. <?php 
  2. class listdir{ 
  3. var $depth; 
  4. var $dirname; 
  5. var $list; 
  6. var $tostring; 
  7. function listdir($dir){ 
  8. $this->dirname=$dir; 
  9. $this->depth=0; 
  10. $this->tostring=”"; 
  11. } 
  12. //把结果保存进多维数组 
  13. function getlist($dir=”"){ 
  14. if($dir==”")$dir=$this->dirname; 
  15. $d=@dir($dir); 
  16. while(false!==($item=$d->read())) 
  17. { 
  18. if($item!=”.”&&$item!=”..”) 
  19. { 
  20. $path=$dir.”/”.$item; 
  21. if(is_dir($path)){ 
  22. $this->depth+=1; 
  23. $this->getlist($path); 
  24. }else{ 
  25. $this->list[$this->depth][]=$item; 
  26. } 
  27. } 
  28. } 
  29. $this->list[$this->depth]['directory']=$dir; 
  30. $this->depth-=1; 
  31. $d->close(); 
  32. return $this->list; 
  33. } 
  34. //字符窜化结果 
  35. function tostring($dir=”"){ 
  36. if($dir==”")$dir=$this->dirname; 
  37. $d=@dir($dir); 
  38. $this->tostring.=”<UL>n”; 
  39. $this->tostring.=”Directory:”.$dir.”n”; 
  40. while(false!==($item=$d->read())) 
  41. { 
  42. if($item!=”.”&&$item!=”..”) 
  43. { 
  44. $path=$dir.”/”.$item; 
  45. if(is_dir($path)){ 
  46. $this->depth+=1; 
  47. $this->tostring($path); 
  48. }else{ 
  49. $this->tostring.=”<LI>”.$item.”</LI>n”; 
  50. } 
  51. } 
  52. } 
  53. $this->depth-=1; 
  54. $d->close(); 
  55. $this->tostring.=”</UL>n”; 
  56. return $this->tostring; 
  57. } 
  58. } 
  59. $wapdir=”jquery”; 
  60. $d=new listdir($wapdir); 
  61. echo $d->tostring(); 
  62. ?> 

要删除一个空的目录很简单~一个,rmdir() 函数就可以搞定,但是要删除一个非空目录,将不能进行快速的删除,必须先将目录中文件删除,但是目录里可能还会有子目录所以要进行递归删除~下面是我的例子~代码如下:

  1. <?php 
  2. function deletedir($dir){ 
  3.       if(!handle=@opendir($dir)){     //检测要打开目录是否存在 
  4.                die("没有该目录"); 
  5.       } 
  6.      while(false !==($file=readdir($handle))){ 
  7.                if($file!=="."&&$file!==".."){       //排除当前目录与父级目录 
  8.                             $file=$dir .DIRECTORY_SEPARATOR. $file; 
  9.                             if(is_dir($file)){ 
  10.                                   deletedir($file); 
  11.                             }else{ 
  12.                                   if(@unlink($file)){ 
  13.                                          echo "文件<b>$file</b>删除成功。<br>"; 
  14.                                   }else{ 
  15.                                           echo  "文件<b>$file</b>删除失败!<br>"; 
  16.                                  } 
  17.                 } 
  18.      } 
  19.     if(@rmdir($dir)){ 
  20.            echo "目录<b>$dir</b>删除成功了。<br>n"; 
  21.     }else{ 
  22.            echo "目录<b>$dir</b>删除失败!<br>n"; 
  23.   } 
  24. } 
  25.  
  26. //测试程序 
  27. $dir="/var/www/test"; 
  28. deletedir($dir); 
  29. ?> 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容