Getting files, folders, file count, folder count in a directory with php


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

Getting files, folders, file count, folder count in a directory with php and writing them to a web page


Copy this code and paste it in your HTML
  1. <?php
  2. $folderCount = $fileCount = 0;
  3. //. means current directory
  4. //if you wanna a learn a folders inside use opendir('path')
  5. if ($handle = opendir('.')) {
  6. while (false !== ($entry = readdir($handle))) {
  7. if ($entry != "." && $entry != "..") {
  8. if (is_dir($entry)) {
  9. echo "Folder => " . $entry . "<br>";
  10. $folderCount++;
  11. } else {
  12. echo "File => " . $entry . "<br>";
  13. $fileCount++;
  14. }
  15. }
  16. }
  17. echo "<br>==============<br>";
  18. echo "Total Folder Count : " . $folderCount . "<br>";
  19. echo "Total File Count : " . $fileCount;
  20. closedir($handle);
  21. }
  22. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.