在 PHP 中获取文件大小的递归函数
Recursive function to get filesize in PHP
我正在开发一个 PHP 函数,它将扫描给定的文件夹和 return 该文件夹中所有文件的总大小。我的问题是,即使它适用于存储在该文件夹根目录中的文件,它也不适用于任何子文件夹中的文件。我的代码是:
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, ".", ",");
}
我取消设置数组的第 0 个和第 1 个元素,因为它们是指向目录的点和双点。任何帮助将不胜感激
您忘记计算子文件夹的大小了。您必须将它添加到 $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;
}
然而,这可能会产生问题,因为您正在使用 number_format
函数。我不会这样做并在收到 get_total_size
函数的结果后添加格式。
您可以使用递归目录迭代器。看看下面的解决方案:
<?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
?>
类 的 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;
您可以试试这个程序。当您检查此文件 is_dir 时,您还必须计算文件大小。当您检查 is_dir 时,您必须将它与根目录连接起来,否则会显示错误。
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, ".", ",");
}
我认为它会很好
编码愉快:)
我正在开发一个 PHP 函数,它将扫描给定的文件夹和 return 该文件夹中所有文件的总大小。我的问题是,即使它适用于存储在该文件夹根目录中的文件,它也不适用于任何子文件夹中的文件。我的代码是:
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, ".", ",");
}
我取消设置数组的第 0 个和第 1 个元素,因为它们是指向目录的点和双点。任何帮助将不胜感激
您忘记计算子文件夹的大小了。您必须将它添加到 $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;
}
然而,这可能会产生问题,因为您正在使用 number_format
函数。我不会这样做并在收到 get_total_size
函数的结果后添加格式。
您可以使用递归目录迭代器。看看下面的解决方案:
<?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
?>
类 的 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;
您可以试试这个程序。当您检查此文件 is_dir 时,您还必须计算文件大小。当您检查 is_dir 时,您必须将它与根目录连接起来,否则会显示错误。
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, ".", ",");
}
我认为它会很好
编码愉快:)