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 - PHP Tutorial
From the course: Learning the Standard PHP Library
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
Inspecting single directories with DirectoryIterator6m 52s
-
Inspecting single directories with FilesystemIterator5m 59s
-
Inspecting directories recursively with RecursiveDirectoryIterator6m 58s
-
Getting information about files with SplFileInfo5m 34s
-
Working on text files with SplFileObject8m
-
Using SplFileObject to convert a CSV to an array4m 25s
-
Exporting data to a download file with SplTempFileObject5m 24s
-
-
-
-
-
-
-