php计算整个目录大小的方法

yipeiwu_com6年前PHP代码库

本文实例讲述了php计算整个目录大小的方法。分享给大家供大家参考。具体实现方法如下:

/**
 * Calculate the full size of a directory
 *
 * @author   Jonas John
 * @version   0.2
 * @param    string  $DirectoryPath  Directory path
 */
function CalcDirectorySize($DirectoryPath) {
  // I reccomend using a normalize_path function here
  // to make sure $DirectoryPath contains an ending slash
  // To display a good looking size you can use a readable_filesize
  // function.
  $Size = 0;
  $Dir = opendir($DirectoryPath);
  if (!$Dir)
    return -1;
  while (($File = readdir($Dir)) !== false) {
    // Skip file pointers
    if ($File[0] == '.') continue; 
    // Go recursive down, or add the file size
    if (is_dir($DirectoryPath . $File))      
      $Size += CalcDirectorySize($DirectoryPath . $File . DIRECTORY_SEPARATOR);
    else 
      $Size += filesize($DirectoryPath . $File);    
  }
  closedir($Dir);
  return $Size;
}
//使用范例:
$SizeInBytes = CalcDirectorySize('data/');

希望本文所述对大家的php程序设计有所帮助。

相关文章

PHP中设置时区,记录日志文件的实现代码

复制代码 代码如下:<html><body><?phpdate_default_timezone_set('Asia/Hong_Kong'); ...

php选择排序法实现数组排序实例分析

本文实例分析了php选择排序法实现数组排序的方法。分享给大家供大家参考。具体分析如下: 选择排序法的基本思路:直接用案例来说明吧,比如有一个数组$arr = array(2,6,3,9)...

图解找出PHP配置文件php.ini的路径的方法

图解找出PHP配置文件php.ini的路径的方法

近来,有不博友问php.ini存在哪个目录下?或者修改php.ini以后为何没有生效?基于以上两个问题,我觉得有必要教一下刚接触PHP的博友们如何找到PHP调用php.ini的路径目录。...

浅谈Eclipse PDT调试PHP程序

1. 下载eclipse,从官网上找就可以了,并确认当前系统中有java环境,即jdk和jre。 2. 安装pdt了,采用的是在线安装,更新地址在默认中已经包含了。只是更新起来比较麻烦。...

php采用curl访问域名返回405 method not allowed提示的解决方法

/** * http测试 * 注:PHP版本5.2以上才支持CURL_IPRESOLVE_V4 * @param $url 网站域名 * @param $type 网站访问协...