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中spl_autoload_register()函数用法实例详解

本文实例分析了PHP中spl_autoload_register()函数用法。分享给大家供大家参考,具体如下: 在了解这个函数之前先来看另一个函数:__autoload。 一、__aut...

PHP正则匹配到2个字符串之间的内容方法

如下所示: $preg= '/xue[\s\S]*?om/i'; preg_match_all($preg,"学并思网址xuebingsi.com",$res); var...

PHP MSSQL 存储过程的方法

复制代码 代码如下:function generateDocCode() { $wf_id = self::WORKFLOW_ID; $doc_code = ""; $link = ms...

php正则表达式(regar expression)

php正则表达式(regar expression)

引言: 在编写处理字符串的程序或网页时,经常会有查找符合某些复杂规则的字符串 的需要。正则表达式就是用于描述这些规则的语法。 例:在判断用户邮件地址格式、手机号码格式或者采集别人网页内容...

PHP Zip解压 文件在线解压缩的函数代码

复制代码 代码如下: /********************** *@file - path to zip file *@destination - destination dire...