PHP遍历并打印指定目录下所有文件实例_PHP

WBOY
Release: 2016-06-01 11:57:18
Original
929 people have browsed it

复制代码 代码如下:
//功能:遍历并打印指定目录下所有文件

function scan_dir($dir_name,$dir_flag=1) {
 static $FILE_COUNT=1;                //记录文件数目 初值为1 目录名称不记
 $FILE_COUNT--;                       //每调用一次scan_dir()函数自减1
 @$dir_handle=opendir($dir_name);     //抑制错误信息显示  便于自定义错误显示
 if(!$dir_handle)
 die("目录打开错误!");
 while(false!==($filename=readdir($dir_handle)))  //文件名为‘0'时,readdir返回 FALSE,判断返回值是否不全等
 {

  $flag=$dir_flag;                 //古怪的 is_dir($filename) ! $filename这个路径必须能够寻到!当$filename不存在或者不是目录时返回false
  if($filename!='.'&&$filename!='..')
  {
   $FILE_COUNT++;                   //不记录当前路径和上一级路径
   while($flag>0&&--$flag)          //负数仍为真
   echo ' ';
   if(is_dir($dir_name.$filename))  //判断 是否为一个目录
   {
    echo ''."".$filename."
";
    scan_dir($dir_name.$filename.'/',$dir_flag+1);      //$dir_flag标志目录树层次
   }
   else
   {
    echo "".$filename."
";
   }
  }
 }
 closedir($dir_handle);                 //关闭目录句柄
 echo "文件总数:".$FILE_COUNT.'
';
}

scan_dir('D:\wamp\www\test\lamp61');  //指定的文件路径
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!