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

PHP uses recursion to calculate folder size, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:10:49732browse

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...
Statement:
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