欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-05-14 13:33:38
...
  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 {
  42. return $size." bytes";
  43. }
  44. else if($size {
  45. $size=round($size/1024,1);
  46. return $size." KB";
  47. }
  48. else if($size {
  49. $size=round($size/(1024*1024),1);
  50. return $size." MB";
  51. }
  52. else
  53. {
  54. $size=round($size/(1024*1024*1024),1);
  55. return $size." GB";
  56. }
  57. }
  58. $path="/home/www/htdocs";
  59. $ar=getDirectorySize($path);
  60. echo "

    路径 : $path

    ";
  61. echo "目录大小 : ".sizeFormat($ar['size'])."
    ";
  62. echo "文件数 : ".$ar['count']."
    ";
  63. echo "目录术 : ".$ar['dircount']."
    ";
  64. //print_r($ar);
  65. ?>
复制代码