Home >Backend Development >PHP Tutorial >How Can I Recursively List Files from All Subdirectories in PHP?
List Files Recursively from All Subdirectories in PHP
In PHP, the list all files in a directory functionality can be extended to retrieve files from subdirectories recursively. This can be valuable for organizing and accessing files within complex directory structures.
Solution:
To recursively list files from all subdirectories, you can use the following code:
<code class="php">foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename) { if ($filename->isDir()) { continue; } echo "$filename\n"; }</code>
Explanation:
Result:
The code creates an array-like structure ($files) containing all files from the current and all subdirectories. The output will be a list of files and their paths:
file.jpg blah.word name.fileext
Additional Resources:
The above is the detailed content of How Can I Recursively List Files from All Subdirectories in PHP?. For more information, please follow other related articles on the PHP Chinese website!