Home > php教程 > php手册 > php使用递归计算文件夹大小

php使用递归计算文件夹大小

WBOY
Release: 2016-06-06 20:15:27
Original
1062 people have browsed it

这篇文章主要介绍了php使用递归计算文件夹大小,代码很简洁使用,这里推荐给大家。

方法很简单,,这里就不多废话了,直接奉上代码:

复制代码 代码如下:


protected function dir_size($dir){
        $dh = opendir($dir);             //打开目录,返回一个目录流
        $size = 0;      //初始大小为0
        while(false !== ($file = @readdir($dh))){     //循环读取目录下的文件
           if($file!='.' and $file!='..'){
            $path = $dir.'/'.$file;     //设置目录,用于含有子目录的情况
                if(is_dir($path)){
                $size += $this->dir_size($path);  //递归调用,计算目录大小
                }elseif(is_file($path)){
                    $size += filesize($path);   //计算文件大小
                }
            }
        }  
        closedir($dh);             //关闭目录流
        return $size;               //返回大小
    }

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template