This article mainly introduces the method of PHP recursively traversing files in a specified directory and counting the number of files. It involves techniques for operating PHP files and directories. It is of great practical value. Friends in need can refer to this article.
The example describes how PHP recursively traverses files in a specified directory and counts the number of files. Share it with everyone for your reference. The specific implementation method is as follows:
<?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>"; //遍历指定文件目录与文件数量结束 ?>
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.
Related recommendations:
php realizes the function of automatically adjusting the font size in a limited area
php uses curl to obtain the Compete statistics website Information method
Analyze the mongodb operation class in php
The above is the detailed content of PHP recursive function implements traversing the directory and number of files under the specified file. For more information, please follow other related articles on the PHP Chinese website!