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

yipeiwu_com5年前PHP代码库

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

/**
 * Calculate the full size of a directory
 *
 * @author   Jonas John
 * @version   0.2
 * @link    http://www.jonasjohn.de/snippets/php/dir-size.htm
 * @param    string  $DirectoryPath  Directory path
 */
function CalcDirectorySize($DirectoryPath) {
  // I reccomend using a normalize_path function here
  // to make sure $DirectoryPath contains an ending slash
  // (-> http://www.jonasjohn.de/snippets/php/normalize-path.htm)
  // To display a good looking size you can use a readable_filesize
  // function.
  // (-> http://www.jonasjohn.de/snippets/php/readable-filesize.htm)
  $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程序设计有所帮助。

相关文章

腾讯微博提示missing parameter errorcode 102 错误的解决方法

本文实例讲述了腾讯微博提示missing parameter errorcode 102 错误的解决方法。分享给大家供大家参考。具体分析如下: 今天在调试腾讯微博接口时,出现一个错误,找...

php中计算中文字符串长度、截取中文字符串的函数代码

在PHP中,我们都知道有专门的mb_substr和mb_strlen函数,可以对中文进行截取和计算长度,但是,由于这些函数并非PHP的核心函数,所以,它们常常有可能没有开启。当然,如果是...

PHP面向对象程序设计中的self、static、parent关键字用法分析

本文实例讲述了PHP面向对象程序设计中的self、static、parent关键字用法.分享给大家供大家参考,具体如下: 看到php里面有关于后期静态绑定的内容,虽然没有完全看懂,但是也...

PHP中多维数组的foreach遍历示例

复制代码 代码如下: <?php //声明一个三维数组 $info=array( "user"=>array( array(1,"zhangsan",20,"nan"), a...

PHP解析url并得到url参数方法总结

PHP 中解析 url 并得到 url 参数 这里介绍两种对url操作的方法: 1、拿到一个完整url后,如何解析该url得到里面的参数。 /** * 解析url中参数信息,返...