Possible Duplicate:
PHP SPL RecursiveDirectoryIterator RecursiveIteratorIterator retrieving the full tree
I am not sure where to start. But I have to get the paths to all files in a folder and all the content of the subfolder in paths too. For example if I had 1 folder that had five folders and each folder had 10 mp3s in it etc… That means my array would have to find 50 paths to these files.
Later lets say I added one more folder and it had 3 folders in it and each folder had 10 images.
My code would now need to find 80 paths and store them in an array.
Does my question make sense?
UPDATE:
My desired out put would be to have all these paths stored in one array.
But I would “LOVE” the code to be dynamic, meaning if I later add 10 more folder and each having 17 subfolder and each folder having a multitude of different content. I would like the array to hold the file paths of all the files. I hppe this makes sense.
What you are looking for is also called recursive directory traversing. Which means, you’re going through all directories and list subdirectories and files in there. If there is a subdirectory it is traversed as well and so on and so forth – so it is recursive.
As you can imagine this is somewhat a common thing you need when you write a software and PHP supports you with that. It offers one
RecursiveDirectoryIteratorso that directories can be recursively iterated and the standardRecursiveIteratorIteratorto do the traversal. You can then easily access all files and directories with a simple iteration, for example viaforeach:This example first of all specifies the directory you want to traverse. I’ve been taking the current one:
The next line of code is a little bit long, it does instantiate the directory iterator and then the iterator-iterator so that the tree-like structure can be traversed in a single/flat loop:
These
$fileinfosare then iterated with a simpleforeach:Inside of it, there is a test to skip all directories from being output. This is done by using the
SplFileInfoobject that is iterated over. It is provided by the recursive directory iterator and contains a lot of helpful properties and methods when working with files. You can as well for example return the file extension, the basename information about size and time and so on and so forth.Finally I just output the pathname that is the full path to the file:
An exemplary output would look like this (here on a windows operating system):
If there is a subdirectory that is not accessible, the following would throw an exception. This behaviour can be controlled with some flags when instantiating the
RecursiveIteratorIterator:I hope this was informative. You can also Wrap this up into a class of your own and you can also provide a
FilterIteratorto move the decision whether a file should be listed or not out of theforeachloop.The power of the
RecursiveDirectoryIteratorandRecursiveIteratorIteratorcombination comes out of its flexibility. What was not covered above are so calledFilterIterators. I thought I add another example that is making use of two self-written of them, placed into each other to combine them.Another change in this usage example is to make use of the
getSubPathname()function that returns the subpath starting from the iteration’s rootpath, so the one you’re looking for.Also I explicitly add the
SKIP_DOTSflag which prevents traversing.and..(technically not really necessary because the filters would filter those as well as they are directories, however I think it is more correct) and return as paths asUNIX_PATHSso the strings of paths are always unix-like paths regardless of the underlying operating system Which is normally a good idea if those values are requested via HTTP later as in your case:This example is similar to the previous one albeit how the
$fileinfosis build is a little differently configured. Especially the part about the filters is new:So the directory iterator is put into a filter and the filter itself is put into another filter. The rest did not change.
The code for these filters is pretty straight forward, they work with the
acceptfunction that is eithertrueorfalsewhich is to take or to filter out:And that’s it again. Naturally you can use these filters for other cases, too. E.g. if you have another kind of directory listing.
And another exemplary output with the
$rootpathcut away:No more
.gitor.svndirectory traversal or listing of files like.builtpathor.project.