Home  >  Article  >  Backend Development  >  PHP recursively traverses all files in the specified directory and counts the number of files

PHP recursively traverses all files in the specified directory and counts the number of files

WBOY
WBOYOriginal
2016-07-25 08:43:291600browse

PHP recursively traverses all files in the specified directory and counts the number of files

  1. //Recursive function implements traversing the directory and number of files under the specified file
  2. function total($dirname,&$dirnum, &$filenum){
  3. $dir=opendir($dirname);
  4. echo readdir($dir)."
    "; //Read files in the current directory
  5. echo readdir($dir)."
    "; //Read the upper-level directory file
  6. while($filename=readdir($dir)){
  7. //What needs to be determined is whether the path under $dirname is a directory
  8. $newfile=$dirname."/".$filename ;
  9. //The is_dir() function determines whether the path of the current script is a directory
  10. if(is_dir($newfile)){
  11. //Traverse the directories or files in its subdirectories through the recursive function
  12. total($newfile, $dirnum,$filenum);
  13. $dirnum++;
  14. }else{
  15. $filenum++;
  16. }
  17. }
  18. closedir($dir);
  19. }
  20. $dirnum=0;
  21. $filenum=0;
  22. total("E :/AppServ/www/phpMyAdmin",$dirnum,$filenum);
  23. echo "Total number of directories: ".$dirnum."
    ";
  24. echo "Total number of files: ".$filenum."
    "; //End of traversing the specified file directory and number of files
  25. ?>
Copy code

PHP


Statement:
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