首页 > php函数 > php 文件目录大小统计函数(自动计算Bytes,KB,MB,GB)

php 文件目录大小统计函数(自动计算Bytes,KB,MB,GB)

文件夹大小统计主要是计算文件夹里面的文件大小然后相加之后再利用函数进行统计了,统计文件大小函数我们使用php filesize函数了,其它的全用目录相关的处理函数,下面一起来看看

计算文件夹的大小,包括子文件夹,格式化输出文件夹大小、文件数、子文件夹数信息。

<?php
//代码也可以用于统计目录数
//格式化输出目录大小 单位:Bytes,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;
}
?>

PHP中计算文件目录大小其实主要是用到"filesize"函数,通过递归的方法计算每个文件的大小,再计算他们的和即是整个文件目录的大小。因为直接返回的文件大小是以字节为单位的,所以我们一般还要经过换算得到我们常见得大小,以下是单位换算的函数:

<?php
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);
?>

后面附一个单位函数

该函数最主要的是根据文件的字节数,判断应当选择的统计单位,也就是说一个文件用某一单位比如MB,那么该文件肯定小于1GB,否则当然要用GB作为单位了,而且文件要大于KB,不然的话得用更小的单位统计。该函数代码如下

<?php
//size()  统计文件大小
function size($byte) {
    if ($byte < 1024) {
        $unit = "B";
    } else if ($byte < 10240) {
        $byte = round_dp($byte / 1024, 2);
        $unit = "KB";
    } else if ($byte < 102400) {
        $byte = round_dp($byte / 1024, 2);
        $unit = "KB";
    } else if ($byte < 1048576) {
        $byte = round_dp($byte / 1024, 2);
        $unit = "KB";
    } else if ($byte < 10485760) {
        $byte = round_dp($byte / 1048576, 2);
        $unit = "MB";
    } else if ($byte < 104857600) {
        $byte = round_dp($byte / 1048576, 2);
        $unit = "MB";
    } else if ($byte < 1073741824) {
        $byte = round_dp($byte / 1048576, 2);
        $unit = "MB";
    } else {
        $byte = round_dp($byte / 1073741824, 2);
        $unit = "GB";
    }
    $byte.= $unit;
    return $byte;
}
function round_dp($num, $dp) {
    $sh = pow(10, $dp);
    return (round($num * $sh) / $sh);
}
?>


本文地址:http://www.phprm.com/function/62457.html

转载随意,但请附上文章地址:-)

标签:opendir

相关文章

发表留言