如何在PHP中将目录树保存到数组?

I'm trying to take a directory with the structure:

top
    folder1
        file1
    folder2
        file1
        file2

And save it into an array like:

array
(
    'folder1' => array('file1'),
    'folder2' => array('file1', 'file2')
)

This way, I can easily resuse the tree throughout my site. I've been playing around with this code but it's still not doing what I want:

private function get_tree()
{
    $uploads  = __RELPATH__ . DS . 'public' . DS . 'uploads';
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($uploads), RecursiveIteratorIterator::SELF_FIRST);
    $output   = array();

    foreach($iterator as $file)
    {
        $relativePath = str_replace($uploads . DS, '', $file);

        if ($file->isDir())
        {
            if (!in_array($relativePath, $output))
                $output[$relativePath] = array();
        }
    }

    return $output;
}

It appears you didn't address the case in which $relativePath is not a directory. You did not describe your output, but i suppose it consists entirely of empty arrays without the desired files ever being inside. Is this correct?

You need to handle the end-case where a file is reached instead of a directory. Take a stab yourself, I can try to post for you later if you are still struggling.

Also, I'm afraid this won't deal with greater depths. Although I am not sure if you need that based on your example.

--EDIT--

This won't build a full tree, as i explained the comment below, but depending on what you're looking for that might be ok. A full tree would take some more significant changes. However, what I post below should at least put the files into the folders.

private function get_tree()
{
    $uploads  = __RELPATH__ . DS . 'public' . DS . 'uploads';
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($uploads), RecursiveIteratorIterator::SELF_FIRST);
    $output   = array();

    foreach($iterator as $file)
    {
        $relativePath = str_replace($uploads . DS, '', $file);

        if ($file->isDir())
        {
            if (!in_array($relativePath, $output))
            {
                $output[$relativePath] = array();
                $lastDir = $relativePath;
            }
        }
        else 
        {
             $p = path_info($file);
             $d = $p['dirname']; $f = $p['basename'];
             $output[$d][] = basename($f); 
        }
    }

    return $output;
}

Im horrible with recursive functions. But i managed to come up with this..not sure it's what you're looking for

function tree($path, $top) {
    $ignore = array('cgi-bin', '.', '..' ); 
    $handle = opendir($path);   

    while( ($file = readdir($handle)) !== false ) {

        if( !in_array( $file, $ignore ) ){              
            if( is_dir("$path/$file") ) {
                $top[$file] = @ tree("$path/$file", $top[$file]);
            } else {
                $top[] = $file;
            }
        }

    }

    closedir( $handle );
    return $top;
}

Assuming your directory tree is

alt text

it outputs

Array
(
    [a] => Array
        (
            [aa] => Array
                (
                    [0] => aa.txt
                )

        )

    [b] => Array
        (
            [0] => bb.txt
        )

    [c] => Array
        (
            [0] => cc.txt
        )

)

But, take a break and go watch the eclipse that happens every 300 and something years. You dont want to say 'no' when your kids, grandkids, great-grandkids ask you 'did you know you were alive when the winter solstice eclipse occurred?? did you see it?????"

:)