Home > Article > Backend Development > How to recursively traverse files in a specified directory and count the number of files in PHP_PHP Tutorial
This article mainly introduces how to use PHP to recursively traverse files in a specified directory and count the number of files, involving PHP File and directory operation skills are of great practical value. Friends who need them can refer to them
The example in this article 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:
?
3 4 513 14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
"; //Read the current directory file
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 ?> |