PHP uses recursive calculation of folder size sample code

Source: Internet
Author: User
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} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.