Listing Files in Directory and Subdirectories in PHP
In PHP, you can utilize directory iteration methods to traverse through a given directory and its subdirectories, effectively retrieving a list of all files within their hierarchical structure.
To achieve this, employ the following steps:
Here's an example that returns an array of file paths:
<code class="php">$files = array(); foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.')) as $filename) { // filter out "." and ".." if ($filename->isDir()) continue; $files[] = $filename->getPathname(); }</code>
The resulting $files array will contain the full paths to all files in the current directory and its subdirectories. For instance, it could resemble:
<code class="php">array("folder1/file.jpg", "folder2/blah.word", "root/name.fileext")</code>
The above is the detailed content of How to List Files in a Directory and its Subdirectories in PHP?. For more information, please follow other related articles on the PHP Chinese website!