php获取文件夹信息的统计函数

原创
2016-07-25 08:56:09 1144浏览
  1. //统计文件夹的相关信息

  2. //统计目录数
  3. //格式化输出目录大小 单位: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 . '//m.sbmmt.com/m/' . $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. //格式化输出信息

  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 "

    您查看的路径 : $path

    ";
  65. echo "目录大小 : ".sizeFormat($ar['size'])."
    ";
  66. echo "文件数 : ".$ar['count']."
    ";
  67. echo "目录数 : ".$ar['dircount']."
    ";

  68. //输出

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

复制代码

例2,php获取文件夹大小的函数

  1. // 获取文件夹大小
  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. // 单位自动转换函数
  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('目录路径'));
  51. ?>
复制代码


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
上一条:php日历代码分享 简单实用的php日历代码 下一条:phpexcel类库实例 支持(excel2003 excel2007)

相关文章

查看更多