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实现的redis缓存类定义与使用方法示例

本文实例讲述了php实现的redis缓存类定义与使用方法。分享给大家供大家参考,具体如下: php+redis缓存类 <?php class redisCache {...

PHP中3种生成XML文件方法的速度效率比较

PHP中3种生成XML文件方法的速度比较 有3种方法,分别是直接写;使用DomDocument;使用SimpleXML;其实还有第4种:使用XMLWriter,不过我没用过,也懒得试了....

php number_format() 函数通过千位分组来格式化数字的实现代码

定义和用法number_format() 函数通过千位分组来格式化数字。 语法number_format(number,decimals,decimalpoint,separator)...

PHP实现的激活用户注册验证邮箱功能示例

本文实例讲述了PHP实现的激活用户注册验证邮箱功能。分享给大家供大家参考,具体如下: 这里将结合实例介绍如何使用PHP+Mysql完成注册帐号、发送激活邮件、验证激活帐号、处理URL链接...

PHP不使用内置函数实现字符串转整型的方法示例

PHP不使用内置函数实现字符串转整型的方法示例

介绍 php字符串类型的数字如果想转成整型的数字,一般我们都是采用系统内置的API去做转换,但如果规定就不让我们去用系统内置的API转换,而是让自己去实现一个函数转换该怎么办?这里我们看...