Home > Backend Development > PHP Tutorial > PHP uses recursion to calculate folder size, _PHP tutorial

PHP uses recursion to calculate folder size, _PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:10:49
Original
757 people have browsed it

php uses recursion to calculate the folder size,

The method is very simple, I won’t go into too much nonsense here, just give you the code:

Copy code The code is as follows:

protected function dir_size($dir){
$ Dh = OpenDIR ($ dir); // Open the directory and return a directory
          $size = 0;                                                                                                                                                                                                      //The initial size is 0
While(false !== ($file = @readdir($dh))){ //Loop to read files in the directory
If($file!='.' and $file!='..'){
               $path = $dir.'/'.$file; If(is_dir($path)){
                      $size += $this->dir_size($path); //Recursive call, calculate directory size
                      }elseif(is_file($path)){
                             $size += filesize($path);                 }
                                                                                                                                              Closedir ($ dh); // Turn off the directors
          return $size;                                                                                                                                                                                                                                                    }





http://www.bkjia.com/PHPjc/932493.htmlwww.bkjia.com

truehttp: //www.bkjia.com/PHPjc/932493.htmlTechArticlephp uses recursion to calculate the folder size. The method is very simple. I won’t go into too much nonsense here. Here is the code: Copy the code as follows: protected function dir_size($dir){ $dh = opend...
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template