views:

37

answers:

3

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

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"  
        )  
    )  
)  
+1  A: 

what about using this snippet

or try this cods

moustafa
This will not create a nested array
Gordon
I have visited your links but these code will not create a nested array. Can you show me another example please?
+1  A: 

Well, to recursively iterate over the RecursiveIterator, you need a RecursiveIteratorIterator (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...

function DirectoryIteratorToArray(DirectoryIterator $it) {
    $result = array();
    foreach ($it as $key => $child) {
        if ($child->isDot()) {
            continue;
        }
        $name = $child->getBasename();
        if ($child->isDir()) {
            $subit = new DirectoryIterator($child->getPathname());
            $result[$name] = DirectoryIteratorToArray($subit);
        } else {
            $result[] = $name;
        }
    }
    return $result;
}

Edit it to work with non-recursive-iterators...

ircmaxell
I have tried your code but it resulted in a fatal error :("Call to undefined method SplFileInfo::hasChildren()"
Ok, I've edited so that it should work better for your case...
ircmaxell
Thanks but now it gives me another error : Maximum function nesting level of '100' reached, aborting! :(
@user366124 Ahh, I forgot to check for the `dot` directories. I'll edit the answer again...
ircmaxell
Ahh, now it works just fine. However, I realize that the way of using SPL is not always faster ( and many other times maybe even slower ) than creating an user-defined function. Anyway, thanks so much for your help :)
+1  A: 

Hi,

here are some urls mentioned how to work with your problem

http://blogs.techrepublic.com.com/programming-and-development/?p=417

http://www.builderau.com.au/program/php/soa/How-do-I-recursively-scan-directories-with-PHP-s-DirectoryIterators-/0,339028448,339289935,00.htm

srinivas
http://www.php.net/~helly/php/ext/spl/directorytreeiterator_8inc-source.htmlThis source code from your links may solve my problem but I don't know how to apply it? Can you give me an example of it usage please?