tags:

views:

32

answers:

2

The following lists the folders, the index.php and the favicon.ico in the directory. I want to see only folders.

Any ideas?

Thanks.

   <?php
     // opens this directory
     $myDirectory = opendir(".");

     // gets each entry
     while($entryName = readdir($myDirectory)) {
       $dirArray[] = $entryName;
     }

     // closes directory
     closedir($myDirectory);

     //  counts elements in array
     $indexCount   = count($dirArray);

     // sorts files
     sort($dirArray);

     // print 'em
     print("<table width='100%' cellspacing='10'>
             <tr>
               </tr>\n");

     // loops through the array of files and print them all
     for($index=0; $index < $indexCount; $index++) {
           if (substr("$dirArray[$index]", 0, 1) != "."){ // don't list hidden files
           print("<tr><td><a href='$dirArray[$index]'>$dirArray[$index]</a></td>");
           print("</tr>\n");
       }
     }
     print("</table>\n");
   ?>
+3  A: 

Use the following:

 // gets each entry
 while($entryName = readdir($myDirectory)) {
   if(is_dir($entryName)) {
     $dirArray[] = $entryName;
   }
 }

I however suggest to use glob() for this kind of operation. e.g.:

glob($dir . '/*', GLOB_ONLYDIR)
halfdan
+1 for using `glob()`
alex
@halfdan Your first solution works perfectly. Thanks a lot.
David
I'm such a newbie with php. I'm not sure how/where to paste the GLOB solution. Does it replace code, follow code?
David
Please have a look at http://php.net/glob
halfdan
A: 

I'm thinking that using the glob function with the directory only flag would be much easier.

$folders = glob('*', GLOB_ONLYDIR); //All directory names are read into an array
Tim Cooper