Recursive Directory Tree
Moderator: General Moderators
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
-
dream2rule
- Forum Contributor
- Posts: 109
- Joined: Wed Jun 13, 2007 5:07 am
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:streamkid wrote:hello, my first post
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
i just started learning php, so your comments on the code are more than welcomeCode: 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> <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 */![]()
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
Reason: php tags
-
gayathriherath
- Forum Newbie
- Posts: 4
- Joined: Mon Mar 17, 2008 4:55 am
Re: Recursive Directory Tree
This is a very nice function.I think your iterative solution is very elegant!
-----------------
Gayathri
-----------------
Gayathri
Re: Recursive Directory Tree
Oooooh, great guy. that's what i exactly need in my this project!!!