【问题标题】:Using filesize() to get the total file size of a directory and it's contents [duplicate]使用 filesize() 获取目录的总文件大小及其内容[重复]
【发布时间】:2013-09-08 12:15:41
【问题描述】:

我正在使用 filesize() 希望返回图像目录的总文件大小。该代码返回的大小为 24mb,我知道这是不正确的,该目录有大约 200+mb 的图像。有什么想法吗?

<?
    $filename = '../uploads';
    echo (filesize($filename) / 1024) . ' mb';
?>

【问题讨论】:

  • 这个网站上已经有几种解决方案。在此处发布新问题之前,您应该查看some existing answers。也许有人已经提供了适合您的解决方案。

标签: php


【解决方案1】:

获取目录大小:source

function dir_size($directory) {
    $size = 0;
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
        $size += $file->getSize();
    }
    return $size;
}

使用以下函数格式化返回值以获得人类可读的值:source

function format_size($size) {
    $mod = 1024;
    $units = explode(' ','B KB MB GB TB PB');
    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }
    return round($size, 2) . ' ' . $units[$i];
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 2021-08-07
  • 2014-01-19
  • 2012-03-08
  • 1970-01-01
  • 2010-11-17
  • 2014-02-24
相关资源
最近更新 更多