当前位置:首页 > PHP教程 > php文件操作 > 列表

php 目录列出所有文件

发布:smiling 来源: PHP粉丝网  添加日期:2014-08-15 14:25:28 浏览: 评论:0 

在php中有很多方法来把目录所有文件列出的代码,如下:

  1. //方法一 
  2.  
  3. $list = scandir("."); 
  4.     $zipname = ""; 
  5.     foreach($list as $file) 
  6.     { 
  7.       if($file=="."||$file=="..")continue; 
  8.       $b=substr($file,-3); 
  9.       if($b==".gz"||$b==".fz") 
  10.       { $zipname = $file; break; } 
  11.     } 
  12.  
  13. //方法二 
  14.  
  15. $d=dir("."); 
  16. echo $d->path.$e; 
  17. while(false !== ($e= $d->read())) { 
  18.     echo "<a href=$e target=_blank >$e</a>"."<br>"; 
  19.     } 
  20. $d->close(); 
  21.  
  22.  
  23. //方法三,最简单的方法 
  24.  
  25. $dirs    = array(); 
  26. foreach(glob("test/*") as $d) 
  27. { 
  28.     if(is_dir($d)) 
  29.     { 
  30.         $dirs[]    = $d; 
  31.     } 
  32. } 
  33. print_r($dirs); 
  34.  
  35. //方法四 
  36.  
  37. glob("test/*", glob_onlydir) ; 
  38.  
  39. //方法五 
  40.  
  41. function clean_dir($path)        { 
  42.         if (!is_dir($path))        { 
  43.                 if (is_file($path))        { 
  44.                         unlink($path); 
  45.                 } 
  46.                 return; 
  47.         }//开源代码phpfensi.com 
  48.         $p=opendir($path); 
  49.         while ($f=readdir($p))        { 
  50.                 if ($f=="." || $f=="..") continue; 
  51.                 clean_dir($path.$f); 
  52.         } 
  53.         rmdir($path); 
  54.         return; 
  55. } 

Tags: php 目录 列出文件

分享到: