PHP recursively traverses all files in the specified directory and counts the number of files
-
- //Recursive function implements traversing the directory and number of files under the specified file
-
- function total($dirname,&$dirnum, &$filenum){
- $dir=opendir($dirname);
- echo readdir($dir)."
"; //Read files in the current directory
- echo readdir($dir)."
"; //Read the upper-level directory file
- while($filename=readdir($dir)){
- //What needs to be determined is whether the path under $dirname is a directory
- $newfile=$dirname."/".$filename ;
- //The is_dir() function determines whether the path of the current script is a directory
- if(is_dir($newfile)){
- //Traverse the directories or files in its subdirectories through the recursive function
- total($newfile, $dirnum,$filenum);
- $dirnum++;
- }else{
- $filenum++;
- }
- }
- closedir($dir);
- }
-
- $dirnum=0;
- $filenum=0;
- total("E :/AppServ/www/phpMyAdmin",$dirnum,$filenum);
- echo "Total number of directories: ".$dirnum."
";
- echo "Total number of files: ".$filenum."
"; //End of traversing the specified file directory and number of files
- ?>
Copy code
|