回显所有文件的总大小
我有这个脚本,除了一个小问题外,它可以工作。基本上,它获取指定目录中所有文件的总大小,但不包括文件夹。
我的目录结构就像...
uploads ->客户01 ->另一个客户 ->其他一些客户
..等。
每个文件夹都包含各种文件,因此我需要脚本来查看“上传”目录并给出所有文件和文件夹的总大小。
<?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";
}
?>
任何帮助将不胜感激。
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...
uploads
-> client 01
-> another client
-> some other client
..ect.
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";
}
?>
Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我会使用一些 SPL 的优点...
Id use some SPL goodness...
最简单的方法是设置一个递归函数
编辑代码中有一个小错误,我现在已经修复了
the simplest way is to setup a recursive function
EDIT there was a small bug in the code that I've fixed now
在此查找关键字目录:http://php.net/ Manual/en/function.filesize.php 有人有一个很棒的函数,可以计算那里的目录大小。
或者,
如果您读取的文件是一个目录,您可能必须递归或循环。
通过 http://php.net/manual/en/function.is-dir.php
find keyword directory inside this : http://php.net/manual/en/function.filesize.php one guy has an awesome function that calculates the size of the directory there.
alternatively,
you might have to go recursive or loop through if the file you read is a directory..
go through http://php.net/manual/en/function.is-dir.php
尝试一下:
不过,请务必验证
$_POST["USER_NAME"]
,否则最终可能会遇到严重的安全错误。 (例如$_POST["USER_NAME"] = "; rm -r /*"
)Try this:
Be sure you validate
$_POST["USER_NAME"]
though, or you could end up with a nasty security bug. (e.g.$_POST["USER_NAME"] = "; rm -r /*"
)