Home > Backend Development > PHP Tutorial > PHP calculates the specified folder information (number of folders, number of files, folder size)

PHP calculates the specified folder information (number of folders, number of files, folder size)

WBOY
Release: 2016-07-25 08:55:52
Original
907 people have browsed it
  1. //Format output directory size unit: Bytes, KB, MB, GB
  2. //Can also be used to count the number of directories
  3. //site bbs.it-home.org
  4. function getDirectorySize( $path)
  5. {
  6. $totalsize = 0;
  7. $totalcount = 0;
  8. $dircount = 0;
  9. if ($handle = opendir ($path))
  10. {
  11. while (false !== ($file = readdir( $handle)))
  12. {
  13. $nextpath = $path . '/' . $file;
  14. if ($file != '.' && $file != '..' && !is_link ($nextpath))
  15. {
  16. if (is_dir ($nextpath))
  17. {
  18. $dircount++;
  19. $result = getDirectorySize($nextpath);
  20. $totalsize += $result['size'];
  21. $totalcount += $result['count'] ;
  22. $dircount += $result['dircount'];
  23. }
  24. elseif (is_file ($nextpath))
  25. {
  26. $totalsize += filesize ($nextpath);
  27. $totalcount++;
  28. }
  29. }
  30. }
  31. }
  32. closedir ($handle);
  33. $total['size'] = $totalsize;
  34. $total['count'] = $totalcount;
  35. $total['dircount'] = $dircount;
  36. return $total;
  37. }
  38. function sizeFormat($size)
  39. {
  40. $sizeStr='';
  41. if($size<1024)
  42. {
  43. return $size." bytes";
  44. }
  45. else if($size<(1024*1024) )
  46. {
  47. $size=round($size/1024,1);
  48. return $size." KB";
  49. }
  50. else if($size<(1024*1024*1024))
  51. {
  52. $size=round ($size/(1024*1024),1);
  53. return $size." MB";
  54. }
  55. else
  56. {
  57. $size=round($size/(1024*1024*1024),1);
  58. return $size." GB";
  59. }
  60. }
  61. $path="/home/www/htdocs";
  62. $ar=getDirectorySize($path);
  63. echo "

    Path: $path";

  64. echo "Directory size: ".sizeFormat($ar['size'])."
    ";
  65. echo "Number of files: ".$ar['count']."
  66. echo "Directory technique: ".$ar['dircount']."
    ";
  67. //print_r($ar);
  68. ?>
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template