Page 1 of 1

How to find all folders from a folder?

Posted: Mon Aug 07, 2006 9:03 am
by kaisellgren
Hi,

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

Thanks!

Posted: Mon Aug 07, 2006 9:33 am
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);
}
?>

Posted: Mon Aug 07, 2006 9:35 am
by feyd
You may also want to look at the Directory Tree thread in Code Snippets.

Posted: Mon Aug 07, 2006 9:39 am
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:

Posted: Mon Aug 07, 2006 10:39 am
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...