How to find all folders from a folder?

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
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

How to find all folders from a folder?

Post by kaisellgren »

Hi,

How do I find all folders from a folder? I will need that for a theme support.

Thanks!
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Straight from the manual with one extra condition added to the second if statement.

Code: Select all

<?php
if ($handle = opendir('.')) { //Leave it as '.' for current dir, or change to dir you want to look in
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && is_dir($file)) {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You may also want to look at the Directory Tree thread in Code Snippets.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Code: Select all

<?php

print_r(glob('*', GLOB_ONLYDIR));

?>
P.S The above will work with PHP 4.3.3 or higher :wink:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

jayshields wrote:Straight from the manual with one extra condition added to the second if statement.

Code: Select all

<?php
if ($handle = opendir('.')) { //Leave it as '.' for current dir, or change to dir you want to look in
   while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && is_dir($file)) {
           echo "$file\n";
       }
   }
   closedir($handle);
}
?>
That's good but it will also give me files... I just want to get the folders...
Post Reply