php 目录列出所有文件
在php中有很多方法来把目录所有文件列出的代码,如下:
- //方法一
- $list = scandir(".");
- $zipname = "";
- foreach($list as $file)
- {
- if($file=="."||$file=="..")continue;
- $b=substr($file,-3);
- if($b==".gz"||$b==".fz")
- { $zipname = $file; break; }
- }
- //方法二
- $d=dir(".");
- echo $d->path.$e;
- while(false !== ($e= $d->read())) {
- echo "$e"."
";- }
- $d->close();
- //方法三,最简单的方法
- $dirs = array();
- foreach(glob("test/*") as $d)
- {
- if(is_dir($d))
- {
- $dirs[] = $d;
- }
- }
- print_r($dirs);
- //方法四
- glob("test/*", glob_onlydir) ;
- //方法五
- function clean_dir($path) {
- if (!is_dir($path)) {
- if (is_file($path)) {
- unlink($path);
- }
- return;
- }//开源代码phpfensi.com
- $p=opendir($path);
- while ($f=readdir($p)) {
- if ($f=="." || $f=="..") continue;
- clean_dir($path.$f);
- }
- rmdir($path);
- return;
- }