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

yipeiwu_com6年前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程序设计有所帮助。

相关文章

最令PHP初学者们头痛的十四个问题

【1】页面之间无法传递变量 get,post,session在最新的PHP版本中自动全局变量是关闭的,所以要从上一页面取得提交过来得变量要使用$_GET['foo'],$_PO...

解析PHP自带的进位制之间的转换函数

bindec() -- 二进制转换为十进制 decbin() -- 十进制转换为二进制 dechex() -- 十进制转换为十六进制 decoct() -- 十进制转换为八进制 hexd...

PHP反射类ReflectionClass和ReflectionObject的使用方法

PHP中的扩展反射类,该扩展用来分析php程序,导出或提取出关于类、方法、属性、参数等的详细信息,包括注释。看一个这样的问题,php类的成员变量没有在类中声明,而是在函数中声明,有什么不...

php中的数组操作函数整理

Array([key =>] value, [key =>] value, [key =>] value, [key =>] value) // key 可以是...

swfupload 多文件上传实现代码

var swfu; window.onload = function() { var settings = { flash_url : "js/swfupload_f9.swf", //...