我正在寻找能够递归显示主文件夹中每个文件夹大小的内容.
这是一个带有CGI-Bin 的LAMP服务器,所以大多数PHP脚本都可以工作,或者任何可以在CGI-Bin中工作的东西.
我的托管公司没有为我提供界面,以查看哪些文件夹占用的空间最多.我不知道互联网上的任何东西,并做了一些搜索,但我没有得到任何结果.
实现图形(GD/ImageMagick)的东西最好但不是必需的.
我的主机仅支持CGI-BIN中的Perl.
奇怪的是,我在Google上找到了许多相关的结果,而且这个结果可能是最完整的.
函数"getDirectorySize"将忽略文件/目录的链接/转移.函数"sizeFormat"将以字节,KB,MB或GB为后缀.
function getDirectorySize($path)
{
$totalsize = 0;
$totalcount = 0;
$dircount = 0;
if ($handle = opendir ($path))
{
while (false !== ($file = readdir($handle)))
{
$nextpath = $path . '/' . $file;
if ($file != '.' && $file != '..' && !is_link ($nextpath))
{
if (is_dir ($nextpath))
{
$dircount++;
$result = getDirectorySize($nextpath);
$totalsize += $result['size'];
$totalcount += $result['count'];
$dircount += $result['dircount'];
}
elseif (is_file ($nextpath))
{
$totalsize += filesize ($nextpath);
$totalcount++;
}
}
}
}
closedir ($handle);
$total['size'] = $totalsize;
$total['count'] = $totalcount;
$total['dircount'] = $dircount;
return $total;
}
function sizeFormat($size)
{
if($size<1024)
{
return $size." bytes";
}
else if($size<(1024*1024))
{
$size=round($size/1024,1);
return $size." KB";
}
else if($size<(1024*1024*1024))
{
$size=round($size/(1024*1024),1);
return $size." MB";
}
else
{
$size=round($size/(1024*1024*1024),1);
return $size." GB";
}
}
Run Code Online (Sandbox Code Playgroud)
$path="/httpd/html/pradeep/";
$ar=getDirectorySize($path);
echo "<h4>Details for the path : $path</h4>";
echo "Total size : ".sizeFormat($ar['size'])."<br>";
echo "No. of files : ".$ar['count']."<br>";
echo "No. of directories : ".$ar['dircount']."<br>";
Run Code Online (Sandbox Code Playgroud)
Details for the path : /httpd/html/pradeep/
Total size : 2.9 MB
No. of files : 196
No. of directories : 20
Run Code Online (Sandbox Code Playgroud)