I'm having a directory with this structure :
- main/
- |- images/
- |-- file1.jpg
- |-- file2.jpg
- |-- file3.jpg
- |- documents/
- |-- private/
- |--- blahblahblah.docx
- |-- test.doc
- |-- test.xls
- |-- test.txt
- |-- private/
- |- images/
I can create a function to complete the work but the RecursiveDirectoryIterator class is much faster and less memory usage this time. How can I use RecursiveDirectoryIterator to list these directory into an array like this :
array(
"main/" => array(
"images/" => array(
"file1.jpg",
"file2.jpg",
"file3.jpg"
),
"documents/" => array(
"private/" => array(
"blahblahblah.docx"
),
"test.doc",
"test.xls",
"test.txt"
)
)
)
Well, to recursively iterate over the
RecursiveIterator
, you need aRecursiveIteratorIterator
(I know it seems redundant, but it's not)...However, for your particular case (Where you're looking to generate a structure rather than just visit all of the nodes), I think regular recursion would be better suited...
Edit it to work with non-recursive-iterators...
Just to record for others, I've turned into a gist a class (
RecursiveDirectoryIterator
) that can read a directory with all its children and output a JSON or just an array.https://gist.github.com/jonataswalker/3c0c6b26eabb2e36bc90
And a tree viewer of the output
http://codebeautify.org/jsonviewer/067c13