欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php--获取文件的磁盘空间

程序员文章站 2022-04-12 19:34:09
<?php
//方法,参数为路径
function dir_size($dir)
{
    //@放在首位,起到忽略错误的用途,打开路径,返回句柄
    @$dh = opendir($dir);
    //初始化容量=0,用来计数
    $size = 0;

    //循环,取出文件夹
    while($file = @readdir($dh))
    {
        //排除上级目录
        if($file != "." && $file != "..")
        {
            
            $path = $dir."/".$file;
            //判断路径是不是目录
            if(is_dir($path))
            {
                echo "$path<br>";
                //类似递归
                $size += dir_size($path);
            }
            //判断是文件
            elseif(is_file($path))
            {
                //输出
                echo "$path&nbsp;&nbsp;&nbsp;&nbsp;(".filesize($path)."byte)<br>";
                //容量累加
                $size += filesize($path);
            }
        }
    }
    //关闭句柄
    @closedir($dh);

    return $size;
}

//定义初始路径
$dir = ".";


$size_dir = dir_size($dir);

echo "total size is $size_dir byte";

?>

 

本文地址:https://blog.csdn.net/modern358/article/details/107325811