From the course: Learning the Standard PHP Library

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Inspecting directories recursively with RecursiveDirectoryIterator

Inspecting directories recursively with RecursiveDirectoryIterator - PHP Tutorial

From the course: Learning the Standard PHP Library

Start my 1-month free trial

Inspecting directories recursively with RecursiveDirectoryIterator

- The DirectoryIterator and FilesystemIterator classes traverse only a single directory. To enter subdirectories, you need to use the RecursiveDirectoryIterator. I've created this page, recursive_directory.php, to test it. So let's create an instance of the RecursiveDirectoryIterator. We'll call it "files." So it's new RecursiveDirectoryIterator then the path. We'll use it to inspect the "common" directory and then we'll have a foreach loop to go through it. So foreach files as file, and then inside the loop, we'll echo the value of file and have a line break. Incidentally, if you're wondering if there's also a RecursiveFilesystemIterator, there isn't. There's only RecursiveDirectoryIterator. But, in fact, it inherits from FilesystemIterator. So it inherits all its configurable options. So let's test this in a browser. And this might come as a bit of a surprise. We're using RecursiveDirectoryIterator, but we're not diving down into each directory. All we've got is a list of what's in…

Contents