我想通过php获取文件夹中文件大小的总和 [英] i want get the sum of files size in folder by php

查看:83
本文介绍了我想通过php获取文件夹中文件大小的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为files的文件夹,如何确定其文件大小的总和?

I have a folder named files, how do I determine the sum of size of it's files?

推荐答案

使用 DirectoryIterator SplFileInfo

$totalSize = 0;
foreach (new DirectoryIterator('/path/to/dir') as $file) {
    if ($file->isFile()) {
        $totalSize += $file->getSize();
    }
}
echo $totalSize;

,并在需要时包括子文件夹:

and in case you need that including subfolders:

$iterator = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('/path/to/dir')
);

$totalSize = 0;
foreach ($iterator as $file) {
    $totalSize += $file->getSize();
}
echo $totalSize;

您可以通过我们提供给

And you can run $totalSize through the code we gave you to format 6000 to 6k for a more human readable output. You'd have to change all 1000s to 1024 though.

这篇关于我想通过php获取文件夹中文件大小的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆