Directory tree

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Renich
Forum Newbie
Posts: 4
Joined: Mon Feb 22, 2010 3:36 pm

Directory tree

Post 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;
    }
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Directory tree

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
Renich
Forum Newbie
Posts: 4
Joined: Mon Feb 22, 2010 3:36 pm

Re: Directory tree

Post 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. ;)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Directory tree

Post by s.dot »

feyd posted a very nice non-recursive directory tree function here that may be useful!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Directory tree

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Renich
Forum Newbie
Posts: 4
Joined: Mon Feb 22, 2010 3:36 pm

Re: Directory tree

Post 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 ;)
Renich
Forum Newbie
Posts: 4
Joined: Mon Feb 22, 2010 3:36 pm

Re: Directory tree

Post by Renich »

scottayy wrote:feyd posted a very nice non-recursive directory tree function here that may be useful!
Thanks for the alternative, Matey! ;=)
Post Reply