Array Based 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
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Array Based Directory Tree

Post by printf »

I've been trying to figure a way to make a directory structured array, where directories are array(s) and files are string(s), keeping the exact format of the directory and all the directories and files it has, no matter how deep it goes! I have looked at hundreds of so called array based tree(s) but none of them, maintain a true array based directory structure.

I know I can do this with eval();, when looping, splitting the directory separator '/' and building the array that way, but I am wondering if there is some way to do it without using eval();


tia

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

Post by feyd »

PHP4 or PHP5?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

You need to create a recursive function (yay!! :)) that opens a directory, loops over all files (except "." and ".." or it will never end), checks if it is a directory with is_dir() and recurses into it if it is, else it adds a string to the currect level of the array.

I can post it if you get stuck cos they can be a bit confusing if you're not used to recursion but you'll be good to try it yourself :)
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

php 5 is fine...

There is this javascript tree menu...

http://www.softcomplex.com/products/tigra_tree_menu/

That expects that the format of the built JavaScript array be exactly how a directory is structured. I'll post what I am using now in a few minutes, I know there must be more simpler way (no eval()), but I can't seem to wrap my head around it.


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

Post by feyd »

two possible solutions I posted here: viewtopic.php?t=47288
Post Reply