Hi,
Could anybody help me out with some code on how to list all the folders in a directory?
Thanks!! :o
$dir = $_SERVER['DOCUMENT_ROOT'].'/path/to/directory'; $dh = dir($dir); while(false !== ($entry = $dh->read())) { if($entry != '.' && $entry != '..') { if(is_dir($dir.'/'.$entry)) echo $entry.' is a folder.<br>'; } }
That should do it....
Thanks. Do you have a more simple version? I am battling to understand the above one.
$dir = $_SERVER['DOCUMENT_ROOT'].'/path/to/directory'; //location of directory you wish to list subdirectories of $dh = dir($dir); // reads the directory into the directory class (More info at [url]http://uk.php.net/manual/en/class.dir.php[/url]) while(false !== ($entry = $dh->read())) { //cycles through the directory contents one at a time and stores the current entry into $entry if($entry != '.' && $entry != '..') { //discards . (the current directory) and .. (the parent directory) if(is_dir($dir.'/'.$entry)) // checks to see if the current entry is a folder echo $entry.' is a folder.<br>'; //prints out that it is a folder } }
You're not going to get much simpler
This is about as simple as it gets:
$directories = glob('*', GLOB_ONLYDIR);
You can now do a foreach loop on $directories to list them, or whatever else it is you want to do with them.
Thanks alot. Just like knowing what I am actually doing some times!!