网站地图    收藏   

主页 > php专栏 > php文件操作 >

php 目录列出所有文件 - php文件操作

来源:自学PHP网    时间:2014-11-28 00:07 作者: 阅读:次

[导读] 在php中有很多方法来把目录所有文件列出的代码,如下: 方法一$list=scandir( );$zipname=;foreach($listas$file){if($file== ||$file== )continue;$b=substr($file,-3);if($b== gz||$b=...

php 目录列出所有文件

在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. } 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论