回显所有文件的总大小

发布于 2024-10-16 15:30:58 字数 732 浏览 4 评论 0原文

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

我的目录结构就像...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

︶ ̄淡然 2024-10-23 15:30:58

我会使用一些 SPL 的优点...

$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>';

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>';
闻呓 2024-10-23 15:30:58

最简单的方法是设置一个递归函数

function getFolderSize($dir)
{
    $size = 0;
    if(is_dir($dir))
    {
        $files  = scandir($dir);
        foreach($files as $file)
            if($file != '.' && $file != '..')
                if(filetype($dir.DIRECTORY_SEPARATOR.$file) == 'dir')
                    $size += getFolderSize($dir.DIRECTORY_SEPARATOR.$file);
                else
                    $size += filesize($dir.DIRECTORY_SEPARATOR.$file);
    }
    return $size;
}

编辑代码中有一个小错误,我现在已经修复了

the simplest way is to setup a recursive function

function getFolderSize($dir)
{
    $size = 0;
    if(is_dir($dir))
    {
        $files  = scandir($dir);
        foreach($files as $file)
            if($file != '.' && $file != '..')
                if(filetype($dir.DIRECTORY_SEPARATOR.$file) == 'dir')
                    $size += getFolderSize($dir.DIRECTORY_SEPARATOR.$file);
                else
                    $size += filesize($dir.DIRECTORY_SEPARATOR.$file);
    }
    return $size;
}

EDIT there was a small bug in the code that I've fixed now

失眠症患者 2024-10-23 15:30:58

在此查找关键字目录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

眼波传意 2024-10-23 15:30:58

尝试一下:

exec("du -s $filepath",$a);
$size = (int)$a[0]; // gives the size in 1k blocks

不过,请务必验证 $_POST["USER_NAME"],否则最终可能会遇到严重的安全错误。 (例如 $_POST["USER_NAME"] = "; rm -r /*"

Try this:

exec("du -s $filepath",$a);
$size = (int)$a[0]; // gives the size in 1k blocks

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 /*")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文