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

相关文章

PHP合并数组+与array_merge的区别分析

主要区别是两个或者多个数组中如果出现相同键名,键名分为字符串或者数字,需要注意 1)键名为数字时,array_merge()不会覆盖掉原来的值,但+合并数组则会把最先出现的值作为最终结果...

PHP字符串比较函数strcmp()和strcasecmp()使用总结

比较字符串是任何编程语言的字符串处理功能中重要的特性之一。在PHP中除了可以使用比较运算符号(==、<或>)加以比较外,还提供了一系列的比较函数,使PHP可以进行更复杂的字符...

PHP+Ajax实时自动检测是否联网的方法

本文实例讲述了PHP+Ajax实时自动检测是否联网的方法。分享给大家供大家参考。具体实现方法如下: html部分代码: <!DOCTYPE html PUBLIC "-//W3...

php empty() 检查一个变量是否为空

empty — 检查一个变量是否为空 Report a bug 描述 bool empty ( mixed $var ) 如果 var 是非空或非零的值,则 empty() 返回 FAL...

php基于协程实现异步的方法分析

本文实例讲述了php基于协程实现异步的方法。分享给大家供大家参考,具体如下: github上php的协程大部分是根据这篇文章实现的:http://nikic.github.io/2012...