Page 1 of 1

Recursive function to load names of directories

Posted: Mon Jun 12, 2006 4:44 pm
by Luke
My brain isn't working at even 50% today, so I'm going to need some help with something that should be easy... anyway, here's my problem:
I am trying to load a list of directories. I would like a recursive function that accepts one "root" folder as an argument and then puts every directory under it into an array like this:

0: root
1: root/sub_directory
2: root/sub_directory/sub_sub_directory
3: root/sub_directory/sub_sub_directory2
4: root/sub_directory2
5: root/sub_directory2/sub_sub_directory

does that make any sense?
Here's what I have to start with:

Code: Select all

if ($open = @opendir($sub_directory)){
			for($i=0;($directory = readdir($open)) != FALSE;$i++)
				if (is_dir($GLOBALS['home_directory']  . $sub_directory ) && $directory != "." && $directory != ".." && !is_hidden_directory($directory))
					$directories[$i] = $directory;
					closedir($open);
				
				if (isset($directories)){
					sort($directories);
					reset($directories);
					//getdetails($directories);
					foreach($directories as $dir){
						echo $dir;
						$this->load_directories($home_directory . $dir);
					}
				}
		}

Posted: Mon Jun 12, 2006 5:03 pm
by feyd
you might want to take a look at the couple of snippets I posted in scottayy's somewhat recent thread found in Code Snippets...