Recursive Directory Tree

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The function returns an associative array. Iterate through it.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

i am very new to PHP Feyd so need your help in doing the above.

Hope you would help!

Regards
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your code will likely need foreach and is_array() along with arrays. I generally won't procure code until I see enough effort on your part.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

i browsed a lot on that but in vain.. :(
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

dream2rule wrote:i browsed a lot on that but in vain.. :(
I'd recommend checking out some books or online tutorials/guides for PHP basics if the manual didn't help you.
streamkid
Forum Newbie
Posts: 3
Joined: Wed May 23, 2007 7:12 am

Post by streamkid »

streamkid wrote:hello, my first post :D

i was looking for a directory tree to use it online, but i didn't find any implementation that would also print a link to the file
so i made one :)

you can see it here:
http://www.streamkid.de/~alex/?cat=stuff

Code: Select all

 
/* you call the function like this: 
$img = array ('images');
ldir('images/', 0, 0, $img, 'images');
this will print the contents of directory images */
        function ldir($path, $level, $m, $struct, $abs) {
                $ignore = array ('cgi-bin', '.', '..', '.htaccess', '.t3hdirectives');
                $dh = @opendir($path);
                while (false !== ($file = readdir($dh))) {
                        if (!in_array($file, $ignore)) {
                                $spaces = str_repeat(' ', ($level*4));
                                $struct[$level] = $abs;
                                $url = '';
                                if ($level > 0) {
                                        for ($i = 0; $i <= $level; $i += 1) {
                                                $url .= $struct[$i];
                                                $url .= '/';
                                            }
                                        $url .= $file;
                                    }
                                else {
                                        $url .= $abs;
                                        $url .= '/';
                                        $url .= $file;
                                    }
                                if (is_dir("$path/$file")) {
                                        echo "<a title="This is a directory. Nothing to link to.">$spaces $file</a>&nbsp;<i>(dir)</i><br/>";
                                        $dir .= '/';
                                        $dir .= $file;
                                        ldir ("$path/$file", ($level + 1), $m, $struct, $file);
                                    } 
                                else {
                                        switch ($m) {
                                                case 0: echo "<a href="?cat=stuff&img=$url">$spaces $file</a><br/>"; break;
                                                case 1: echo "<a href="$url">$spaces $file</a><br/>";             break;
                                                case 2: echo "<a href="$url">$spaces $file</a><br/>";                break;
                                            }
                                    }
                            }
                    }
                closedir($dh);
            }
/* you can completely delete $m and the switch statement, i just used it because depending on the type the url differs */
 
i just started learning php, so your comments on the code are more than welcome :)
Watching traffic on my site, I see a lot of people coming from this thread to my site to see the listing script in action. I want to make an upate:
An updated version of the code can be found here:
http://streamkid.net/blog/programming/5 ... 0/12/2007/
and you can see the script in action here:
http://streamkid.net/~streamkid/

Thanks for the interest. :-)
Last edited by Weirdan on Sat May 03, 2008 7:36 am, edited 1 time in total.
Reason: php tags
gayathriherath
Forum Newbie
Posts: 4
Joined: Mon Mar 17, 2008 4:55 am

Re: Recursive Directory Tree

Post by gayathriherath »

This is a very nice function.I think your iterative solution is very elegant!

-----------------
Gayathri
pkphp
Forum Newbie
Posts: 12
Joined: Mon Sep 20, 2010 1:20 am

Re: Recursive Directory Tree

Post by pkphp »

Oooooh, great guy. that's what i exactly need in my this project!!!
Post Reply