Home >Backend Development >PHP Tutorial >PHP statistical function to obtain folder information

PHP statistical function to obtain folder information

WBOY
WBOYOriginal
2016-07-25 08:56:091503browse
  1. //Statistical folder-related information

  2. //Number of statistical directories
  3. //Format output directory size unit: Bytes, KB, MB, GB
  4. function getFolderSize( $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 = getFolderSize($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. //Format output information

  39. function sizeFormat($size)
  40. {
  41. $sizeStr='';
  42. if($size<1024)
  43. {
  44. return $size." bytes" ;
  45. }
  46. else if($size<(1024*1024))
  47. {
  48. $size=round($size/1024,1);
  49. return $size." KB";
  50. }
  51. else if($size< (1024*1024*1024))
  52. {
  53. $size=round($size/(1024*1024),1);
  54. return $size." MB";
  55. } bbs.it-home.org
  56. else
  57. {
  58. $size=round($size/(1024*1024*1024),1);
  59. return $size." GB";
  60. }
  61. }
  62. $path="/var/www";
  63. $ar= getFolderSize($path);
  64. echo "

    The path you are viewing: $path

    ";
  65. echo "Directory size: ".sizeFormat($ar['size'])."echo "Number of files: ".$ar['count']."
    ";
  66. echo "Number of directories: ".$ar['dircount']."
    ";< ;/p>
  67. //Output

  68. //print_r($ar);
  69. ?>

Copy code

Example 2, PHP function to get the folder size

  1. // Get the folder size
  2. function getDirSize($dir)
  3. {
  4. $handle = opendir($dir);
  5. while (false!==($FolderOrFile = readdir($ handle)))
  6. {
  7. if($FolderOrFile != "." && $FolderOrFile != "..")
  8. {
  9. if(is_dir("$dir/$FolderOrFile"))
  10. {
  11. $sizeResult += getDirSize ("$dir/$FolderOrFile");
  12. }
  13. else
  14. {
  15. $sizeResult += filesize("$dir/$FolderOrFile");
  16. }
  17. }
  18. }
  19. closedir($handle);
  20. return $sizeResult;
  21. }
  22. // Automatic unit conversion function
  23. function getRealSize($size)
  24. {
  25. $kb = 1024; // Kilobyte
  26. $mb = 1024 * $kb; // Megabyte
  27. $gb = 1024 * $mb; // Gigabyte
  28. $tb = 1024 * $gb; // Terabyte
  29. if($size < $kb)
  30. {
  31. return $size." B";
  32. }
  33. else if($size < $mb)
  34. {
  35. return round($size/$kb,2)." KB";
  36. }
  37. else if($size < $gb)
  38. {
  39. return round($size/$mb,2)." MB";
  40. }
  41. else if($size < $tb)
  42. {
  43. return round($size/$gb,2)." GB";
  44. }
  45. else
  46. {
  47. return round($size/$tb,2). " TB";
  48. }
  49. }
  50. echo getRealSize(getDirSize('directory path'));
  51. ?>
Copy code


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