回应所有文件的组合大小 [英] Echo the combined size of all files

查看:100
本文介绍了回应所有文件的组合大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个脚本,除了一个小问题。基本上它获得了指定目录中所有文件的总大小,但不包括文件夹。

I have this script which works except for one small problem. Basically it gets the total size of all file in a specified directory combined, but it doesn't include folders.

我的目录结构就像...

My directory structure is like...

上传
- >客户端01
- >另一个客户端
- >其他客户端

uploads -> client 01 -> another client -> some other client

.. ect。

每个文件夹包含各种文件,所以我需要脚本来查看'uploads'目录,并给出我所有文件和文件夹的大小

Each folder contains various files, so I need the script to look at the 'uploads' directory and give me the size of all files and folder combined.

<?php      
$total = 0; //Total File Size
//Open the dir w/ opendir();
$filePath = "uploads/" . $_POST["USER_NAME"] . "/";
$d = opendir( $filePath ); //Or use some other path.
    if( $d ) {
while ( false !== ( $file = readdir( $d ) ) ) { //Read the file list
   if (is_file($filePath.$file)){
$total+=filesize($filePath.$file);
   }
}
closedir( $d ); //Close the direcory
    echo number_format($total/1048576, 2);
    echo ' MB<br>';
}
else {
    echo "didn't work";
}
?>

任何帮助将不胜感激。

推荐答案

Id使用一些SPL的好处...

Id use some SPL goodness...

$filePath = "uploads/" . $_POST["USER_NAME"];

$total = 0;
$d = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($filePath), 
  RecursiveIteratorIterator::SELF_FIRST
);

foreach($d as $file){
  $total += $file->getSize();
}

echo number_format($total/1048576, 2);
echo ' MB<br>';

这篇关于回应所有文件的组合大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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