Page 1 of 1

Directory tree

Posted: Tue Feb 23, 2010 3:10 am
by Renich
Hello,

I need hbelp with this. I'd like to get a directory tree in a multidimensional array like:

Code: Select all

<$x = array(
    dir1 => array(
        0 => file1
        1 => file2
        2 => array(
            dir2 = array(
                0 = file3
                1 = file4
                2 = file5
                4 = file6
            )
        )
    )
)
Here's my funciton. It would be really cool if the searchFiles() would be recursive ;)

Code: Select all

class Default_Model_SongList
{
    private $path;
    private $files;
    private $data;
    protected $songs;
    
    public function __construct($path = null)
    {
        // filter path
        $filter = new Zend_Filter_RealPath();
        $this->path = $filter->filter($path);
        
        // check if path is valid
        if (!is_dir($this->path) || null === $this->path) {
            require_once 'Zend/File/Transfer/Exception.php';
            throw new Wrll_Exception("That's no path there, matey or, just maybe, you might not have permission!", 201);
        }
        
        // get file list
        $this->songs = $this->searchFiles($this->path);
        
        // filter the songs
    }
    
    public function getSongList()
    {
        return $this->songs;
    }
    
    private function searchFiles($path)
    {
        if (is_dir($path)) {
            $d = dir($path);
            
            while (false !== ($file = $d->read())) {
                if ($file != '.' && $file != '..') {
                    $this->files[$d->path][] = $file;
                }
            }
            
            foreach ($this->files as $fn) {
                foreach ($fn as $n => $file) {
                    if (is_dir($d->path . '/' . $file)) {
                        $this->files[$d->path][$n] = ???;
                    }
                }
            }
        }
 
        return $this->files;
    }
}

Re: Directory tree

Posted: Tue Feb 23, 2010 3:57 am
by VladSun
You need to recursively call the searchFiles() method on line 45. Its argument must be the directory you've found.

A few thing to mention:
- you don't need to return $this->files in this case - you have it in your object, so you can always access it later.
- I'd prefer glob() to iterate through files/directories

Re: Directory tree

Posted: Tue Feb 23, 2010 10:54 am
by Renich
VladSun wrote:You need to recursively call the searchFiles() method on line 45. Its argument must be the directory you've found.

A few thing to mention:
- you don't need to return $this->files in this case - you have it in your object, so you can always access it later.
- I'd prefer glob() to iterate through files/directories
Thank you so much for the reply. Yes, I know, but the result is something really odd; if you care to try it, hehe.

Yes, I will consider using glob(). I tried using it at first but ran into problems... Maybe a second approach will do. I'm concerned about speed and resources too. ;)

Re: Directory tree

Posted: Tue Feb 23, 2010 2:18 pm
by s.dot
feyd posted a very nice non-recursive directory tree function here that may be useful!

Re: Directory tree

Posted: Tue Feb 23, 2010 2:23 pm
by AbraCadaver
Several problems in the code, but this should work:

Code: Select all

function searchFiles($path)
    {
        $files = array();
 
        if (is_dir($path)) {
            $d = dir($path);
           
            while (false !== ($file = $d->read())) {
                if ($file != '.' && $file != '..') {
                    if (is_dir($d->path . '/' . $file)) {
                        $files[basename($path)][] = $this->searchFiles($d->path . '/' . $file);
                    } else {
                        $files[basename($path)][] = $file;
                    }
                }
            }
        } 
        return $files;
    }
One of the main problems was that you were setting $this->files in the loop, so if you also returned it and set it to a new array of $this->files recursively, then it would just keep growing.

Re: Directory tree

Posted: Tue Feb 23, 2010 3:01 pm
by Renich
AbraCadaver wrote:One of the main problems was that you were setting $this->files in the loop, so if you also returned it and set it to a new array of $this->files recursively, then it would just keep growing.
Whoa! thank you, so much, for the help!

I'm new to OOP so I am prone to committing stupid mistakes. Thanks for the constructive critique ;)

Re: Directory tree

Posted: Tue Feb 23, 2010 3:02 pm
by Renich
scottayy wrote:feyd posted a very nice non-recursive directory tree function here that may be useful!
Thanks for the alternative, Matey! ;=)