简体   繁体   English

用于在PHP中获取文件大小的递归函数

[英]Recursive function to get filesize in PHP

I am working on a PHP function that will scan a given folder and return the total size of all the files in the folder. 我正在开发一个PHP函数,它将扫描给定的文件夹并返回文件夹中所有文件的总大小。 My issue is that, even though it works for files stored in the root of that folder, it doesn't work for files in any subfolder. 我的问题是,即使它适用于存储在该文件夹根目录中的文件,它也不适用于任何子文件夹中的文件。 My code is: 我的代码是:

function get_total_size($system)
{
    $size = 0;
    $path = scandir($system);
    unset($path[0], $path[1]);
    foreach($path as $file)
    {
        if(is_dir($file))
        {
            get_total_size("{$system}/{$file}");
        }
        else
        {
            $size = $size + filesize("{$system}/{$file}");
        }
    }
    $size = $size / 1024;
    return number_format($size, 2, ".", ",");
}    

I'm unsetting the 0th and 1st elements of the array since these are the dot and the double dot to go up a directory. 我没有设置数组的第0个和第1个元素,因为这些是点和双点来上一个目录。 Any help would be greatly appreciated 任何帮助将不胜感激

You forgot to count the size of the subfolders. 你忘了计算子文件夹的大小。 you have to add it to the $size variable. 你必须将它添加到$size变量。

function get_total_size($system)
{
    $size = 0;
    $path = scandir($system);
    unset($path[0], $path[1]);
    foreach($path as $file)
    {
        if(is_dir($file))
        {
            $size += get_total_size("{$system}/{$file}"); // <--- HERE
        }
        else
        {
            $size = $size + filesize("{$system}/{$file}");
        }
    }
    return $size;
}    

This might however give a problem because you are using the number_format function. 但是,这可能会产生问题,因为您使用的是number_format函数。 I would not do this and add the formatting after receiving the result of the get_total_size function. 我不会这样做,并在收到get_total_size函数的结果后添加格式。

you can use recursive directory iterator for the same. 你可以使用递归目录迭代器。 Have a look on below solution: 看看下面的解决方案:

<?php
$total_size = 0;
$di = new RecursiveDirectoryIterator('/directory/path');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
   if($file->isFile()) {
        echo $filename . ' - ' . $file->getSize() . ' bytes <br/>';
        $total_size += $file->getSize();
    }
}

echo $total_size; //in bytes
?>

You may try this procedure. 您可以尝试此过程。 When you check this file is_dir then you have to count the file size also. 当您检查此文件is_dir时,您还必须计算文件大小。 And when you check is_dir you have to concat it with root directory otherwise it show an error. 当你检查is_dir时,你必须用root目录连接它,否则它会显示错误。

function get_total_size($system)
{
    $size = 0;
    $path = scandir($system);
    unset($path[0], $path[1]);
    foreach($path as $file)
    {
        if(is_dir($system.'/'.$file))
        {
            $size+=get_total_size("{$system}/{$file}");
        }
        else
        {
            $size = $size + filesize("{$system}/{$file}");
        }
    }
    $size = $size / 1024;
    return number_format($size, 2, ".", ",");
}  

I think it will work fine 我认为它会正常工作

Happy coding :) 快乐编码:)

The recursiveIterator family of classes could be of use to you. recursiveIterator类的系列可能对您有用。

function filesize_callback( $obj, &$total ){
    foreach( $obj as $file => $info ){
        if( $obj->isFile() ) {
            echo 'path: '.$obj->getPath().' filename: '.$obj->getFilename().' filesize: '.filesize( $info->getPathName() ).BR;
            $total+=filesize( $info->getPathName() );
        } else filesize_callback( $info,&$total );
    }
}
$total=0;
$folder='C:\temp';
$iterator=new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $folder, RecursiveDirectoryIterator::KEY_AS_PATHNAME ), RecursiveIteratorIterator::CHILD_FIRST );
call_user_func( 'filesize_callback', $iterator, &$total );

echo BR.'Grand-Total: '.$total.BR;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM