The programming technique called by the program itself is called recursion (recursion). Recursion as an algorithm is widely used in programming language. A procedure or function has a method of directly or indirectly invoking itself in its definition or description, which usually transforms a large and complex problem layer into a smaller problem similar to the original problem, and the recursive strategy can describe the repeated computations needed in the process of solving the problems by a small number of programs. Greatly reduces the code volume of the program. The ability to recursion is to define an infinite set of objects with limited statements. In general, recursion requires boundary conditions, recursive forward segments, and recursive return segments. Recursion advances when boundary conditions are not met, and returns recursively when the boundary conditions are met.
This article mainly introduces PHP using recursive calculation folder size, the code is very concise use, here is recommended to everyone. directly on the code:
The code is as follows:
protected function dir_size ($dir) {$DH = Opendir ($dir); Open Directory, return a directory stream $size = 0; The initial size is 0 while (false!== ($file = @readdir ($DH))) {//loop read the file under directory if ($file! = '. ' and $file! = ' ... ') {$path = $dir. '/'. $file; Set directory, for cases with subdirectories if (Is_dir ($path)) {$size + = $this->dir_size ($path); Recursive invocation, calculating the directory size}elseif (Is_file ($path)) {$size + = FileSize ($path); Calculate File Size}}} closedir ($DH); Close the directory stream return $size; Return size}