当前位置:Gxlcms > PHP教程 > php计算指定文件夹信息(文件夹数,文件数,文件夹大小)

php计算指定文件夹信息(文件夹数,文件数,文件夹大小)

时间:2021-07-01 10:21:17 帮助过:65人阅读

  1. //格式化输出目录大小 单位:Bytes,KB,MB,GB
  2. //也可以用于统计目录数
  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

    ";
  64. echo "目录大小 : ".sizeFormat($ar['size'])."
    ";
  65. echo "文件数 : ".$ar['count']."
    ";
  66. echo "目录术 : ".$ar['dircount']."
    ";
  67. //print_r($ar);
  68. ?>

人气教程排行