我正在寻找能够递归显示主文件夹中每个文件夹大小的内容。
这是一个带有CGI-Bin 的LAMP服务器,因此大多数PHP脚本都可以工作,或者任何可以在CGI-Bin中工作的东西。
我的托管公司没有为我提供界面,以查看哪些文件夹占用的空间最多。
实现图形(GD / ImageMagick)的东西最好,但不是必需的。
我的主机只支持CGI-BIN中的Perl。
发布于 2018-07-20 17:43:09
奇怪的是,我在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";
}
}
$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>";
Details for the path : /httpd/html/pradeep/
Total size : 2.9 MB
No. of files : 196
No. of directories : 20
发布于 2018-07-20 18:23:43
如果你拥有shell访问权限,则可以运行以下命令
$ du -h
或者,如果PHP被配置为允许执行,也可以使用它:
<?php $d = escapeshellcmd(dirname(__FILE__)); echo nl2br(`du -h $d`) ?>
https://stackoverflow.com/questions/-100000388
复制相似问题