count files in a given dir


/ Published in: PHP
Save to your folder(s)

count files in a given dir


Copy this code and paste it in your HTML
  1. <?php
  2. function count_files($name) {
  3. $dirname = "galleries/$name";
  4. if(is_dir($dirname))
  5. $dir_handle = opendir($dirname);
  6. if(!$dir_handle)
  7. return false;
  8.  
  9. $files = 0;
  10.  
  11. while($file = readdir($dir_handle)) {
  12. if($file != "." and $file != "..") {
  13. if(!is_dir($dirname . "/" . $file))
  14. $files++;
  15. else
  16. $files += count_files($dirname . "/" . $file);
  17. }
  18. }
  19.  
  20. closedir($dir_handle);
  21.  
  22. return $files;
  23. }
  24. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.