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获取数组中单列值的方法

本文实例讲述了PHP获取数组中单列值的方法。分享给大家供大家参考,具体如下: PHP中获取数组中单列的值如下: 利用PHP中的数组函数 array_column():返回数组中某个单列的...

MAC下通过改apache配置文件切换php多版本的方法

前言 前段时间,在自己的电脑上升级了php,php7.0虽然有部分更新,速度也提升了不少,但最近在做微信开发时,发现很多引擎不支持php7,于是想能不能安装两个版本进行切换,百度了很多方...

处理单名多值表单的详解

就使用一个简单的可多选的select:复制代码 代码如下:<?phpecho<<<EOT<form action="" method=get>&nbs...

php使用curl实现简单模拟提交表单功能

php使用curl实现简单模拟提交表单功能

php 使用curl 进行简单模拟提交表单,供大家参考,具体内容如下 //初始化curl $ch = curl_init(); $url = 'xxx'; $option = [...

php合并数组array_merge函数运算符加号与的区别

array_merge在参考手册中的说明如下: array_merge() 将两个或多个数组的单元合并起来,一个数组中的值附加在前一个数组的后面。返回作为结果的数组。 如果输入的数组中有...