Home  >  Article  >  Backend Development  >  PHP recursively obtains files in a directory (including subdirectories) encapsulation class sharing_PHP tutorial

PHP recursively obtains files in a directory (including subdirectories) encapsulation class sharing_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:18:33925browse

The code is as follows:

Copy code The code is as follows:

function readFileFromDir($dir) {
if (!is_dir($dir) ) {
            return false; ) {
                                                                                                                                                                             >        $file = $dir . DIRECTORY_SEPARATOR . $file;
                                                                                                                                                                                         $file />';
} elseif (is_dir($file)) {
          readFileFromDir($file);
Calling method:




Copy code

The code is as follows:


$dir = '/home/www/test';
readFileFromDir($ dir);

If you look at the PHP manual, there is another method scandir that can also be used, but this method will get all the files in a single-level directory at once and store them in an array. If there are many files in the directory, it will get stuck.

http://www.bkjia.com/PHPjc/621658.htmlwww.bkjia.com
true

http: //www.bkjia.com/PHPjc/621658.html

TechArticle
The code is as follows: Copy the code as follows: function readFileFromDir($dir) { if (!is_dir($dir)) { return false; } //Open directory $handle = opendir($dir); while (($file = readdir($ha...

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