php计算指定文件夹信息(文件夹数,文件数,文件夹大小)
发布时间:2017-06-03 编辑:jiaochengji.com
分享一例php代码,用于统计指定文件夹的信息,包括文件夹数、文件数、文件夹大小等,有需要的朋友参考下。
本节内容:
统计文件夹信息的php代码。
此代码,实现如下功能:
计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。
例子:
复制代码 代码示例:
<?
//格式化输出目录大小 单位:Bytes,KB,MB,GB
//也可以用于统计目录数
//site www.jbxue.com
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)
{
$sizeStr='';
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="/home/www/htdocs";
$ar=getDirectorySize($path);
echo "<h4>路径 : $path</h4>";
echo "目录大小 : ".sizeFormat($ar['size'])."<br>";
echo "文件数 : ".$ar['count']."<br>";
echo "目录术 : ".$ar['dircount']."<br>";
//print_r($ar);
?>
//格式化输出目录大小 单位:Bytes,KB,MB,GB
//也可以用于统计目录数
//site www.jbxue.com
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)
{
$sizeStr='';
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="/home/www/htdocs";
$ar=getDirectorySize($path);
echo "<h4>路径 : $path</h4>";
echo "目录大小 : ".sizeFormat($ar['size'])."<br>";
echo "文件数 : ".$ar['count']."<br>";
echo "目录术 : ".$ar['dircount']."<br>";
//print_r($ar);
?>
您可能感兴趣的文章:
php计算指定文件夹信息(文件夹数,文件数,文件夹大小)的代码
php 读取文件夹与文件夹中文件的函数
PHP 创建文件夹的自定义函数
VBS脚本运行库手册(2)-管理文件夹
VBS脚本运行库手册(3)-文件夹属性
ASP 3.0高级编程(二十一)
常用dos操作命令
php获取文件夹大小的函数
php计算指定文件夹信息(文件夹数,文件数,文件夹大小)
什么是FSO?
上一篇:PHP采集器的简单示例代码
下一篇:php判断正整数的简单示例
[关闭]