Home > php教程 > php手册 > body text

PHP递归遍历指定目录的文件并统计文件数量的方法,

WBOY
Release: 2016-06-13 09:10:05
Original
847 people have browsed it

PHP递归遍历指定目录的文件并统计文件数量的方法,

本文实例讲述了PHP递归遍历指定目录的文件并统计文件数量的方法。分享给大家供大家参考。具体实现方法如下:

<&#63;php
//递归函数实现遍历指定文件下的目录与文件数量
function total($dirname,&$dirnum,&$filenum){
  $dir=opendir($dirname);
  echo readdir($dir)."<br>"; //读取当前目录文件
  echo readdir($dir)."<br>"; //读取上级目录文件
  while($filename=readdir($dir)){
    //要判断的是$dirname下的路径是否是目录
    $newfile=$dirname."/".$filename;
    //is_dir()函数判断的是当前脚本的路径是不是目录
    if(is_dir($newfile)){
      //通过递归函数再遍历其子目录下的目录或文件
      total($newfile,$dirnum,$filenum);
      $dirnum++;
    }else{
      $filenum++;
    }
  }
  closedir($dir);
}
$dirnum=0;
$filenum=0;
total("E:/AppServ/www/phpMyAdmin",$dirnum,$filenum);
echo "目录总数:".$dirnum."<br>";
echo "文件总数:".$filenum."<br>";
//遍历指定文件目录与文件数量结束
&#63;>
Copy after login

希望本文所述对大家的php程序设计有所帮助。

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 Recommendations
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!