-
-
/** - * Recursively obtain files in directories and subdirectories
- * by bbs.it-home.org
- */
function readFileFromDir($dir) {
- if (!is_dir($dir)) {
- return false;
- }
- //Open the directory
- $handle = opendir($dir);
- while (($file = readdir($handle)) !== false) {
- //Exclude the current directory and the previous directory A directory
- if ($file == "." || $file == "..") {
- continue;
- }
- $file = $dir . DIRECTORY_SEPARATOR . $file;
- //If it is a file, print it out , otherwise call recursively
- if (is_file($file)) {
- print $file . '
';
- } elseif (is_dir($file)) {
- readFileFromDir($file);
- }
- }
- }
-
Copy code
Calling method:
-
- $dir = '/var/www/test';
- readFileFromDir($dir);
-
Copy the code
View the php manual and find that a method scandir can also be used. This method will be used once Get all the files in a single-level directory and store them in an array.
This method is not suitable when there are many files in the directory.
|