Forum Moderators: coopster

Message Too Old, No Replies

Simple Script For directories and Subdirectories

         

havoc

8:18 am on Jan 24, 2007 (gmt 0)

10+ Year Member



I am doing up my own gallery script and have sorted out most of the issues but i am having trouble what seems to be a very easy issue . Must be because i have not coded for about 10 months. oh well

What i need is a script which will read directories and sub directories and out put them to a menu

example

Directory structure

MainDir
¦-SubDir
¦---Subsubdir
¦-Subdir2
¦---Subsubdir2

I would like it output like that too so u can see the subdir is under the parent dir. the best i got so far is it to put the list of dirs but not in order or in any structure. I am sure there has to be a function to do it somewhere? :)

mcibor

10:06 am on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't think there is a specific function for that. What you need is a recurent function that will read dirs and show the depth:

something like:

[pre]function tree($dir, $depth, &$tree) {
if ($handle = opendir($dir)) {
$depth++;

while (false!== ($file = readdir($handle))) {
if(!is_dir("$dir/$file") continue;
$tree[] = array("dir" => $file, "depth" => $depth)echo "$file\n";
tree("$dir/$file", $depth, &$tree);
}
closedir($handle);
}

and call the function as:
$basedir = "/var/www/htdocs/example.com";
$depth = 0;
$tree = array();
tree($basedir, $depth, $tree);

then in $tree[0]['dir'] you have the directory
and in $tree[0]['depth'] you have the depth

In the beginning don't forget to create a watchdog - eg. end script on 1000 dir, so you don't have a neverending loop

Hope this is what you looked for
Michal

havoc

10:08 am on Jan 24, 2007 (gmt 0)

10+ Year Member



Thanks mate , will give this a go . I can;t believe how hard it is to program once you stop doing it for a while :)

mcibor

10:24 am on Jan 24, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I haven't tried that yet, but I know what you mean. I once returned to script I wrote two years before and it took me 2 hours to find out why I used there one specific if.
To say the truth first I remembered what it was for, than to get it from the coding ;)

Michal

PS. I wonder how many errors there were ;)