Getting a list of files in a directory is extremely easy. Traditionally, developers have used the glob()
function for this purpose. To recursively get a list of all files and directories from a specific point in a directory tree is more problematic. This recipe takes advantage of an (SPL Standard PHP Library) class RecursiveDirectoryIterator
, which will serve this purpose admirably.
What this class does is to parse the directory tree, finding the first child, then it follows the branches, until there are no more children, and then it stops! Unfortunately this is not what we want. Somehow we need to get the RecursiveDirectoryIterator
to continue parsing every tree and branch, from a given starting point, until there are no more files or directories. It so happens there is a marvelous class, RecursiveIteratorIterator
, that does exactly that. By wrapping RecursiveDirectoryIterator
inside RecursiveIteratorIterator,
we accomplish a complete traversal of any directory...