Recursive function to load names of directories

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
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Recursive function to load names of directories

Post 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);
					}
				}
		}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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...
Post Reply