<<< How to get all subdirectory name in a directory >>>

Discussion in 'PHP' started by junandya, Jan 2, 2009.

  1. #1
    Hello all,

    i have a folder called with firm, which is inside it i have several subfolder, let say:

    government
    University
    NGO
    Church

    But the problem is, the content of firm folder is not static, i mean, it can be added with another subfolder by the time being.

    I need to get them to a dropdown menu automatically. maybe like this
    <select name="folder">
    <option value="">government</option>
    <option value="">University</option>
    <option value="">NGO</option>
    <option value="">Church</option>
    </select>
    
    PHP:
    So the option in this selection has to be increase automatically as many as firm folder.

    The point is how can i get all of subfolder name automatically? should i use dirname function, but how?

    Best regards
    Junandya
     
    junandya, Jan 2, 2009 IP
  2. manjifera

    manjifera Well-Known Member

    Messages:
    232
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    118
  3. fairuz.ismail

    fairuz.ismail Peon

    Messages:
    232
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try this, hope it helps;

    <?php
    
    $dirs = array();
    $dir = "firm";
    $d = dir($dir);
    while(false != ($entry = $d->read())){
       if(is_dir(implode('/', array($dir, $entry)))){
          if(($entry != '.') && ($entry != '..')){
             $dirs[] = $entry;
          }
       }
    }
    
    echo '<select name="folder">';
    foreach($dirs as $dir){
        echo sprintf("<option value=\"%s\">%s</option>",$dir,$dir);
    }
    echo '</select>';
    
    ?>
    PHP:
     
    fairuz.ismail, Jan 3, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    <?php
    
    chdir('/put/the/directory/here');
    $subfolders = glob('*', GLOB_ONLYDIR);
    
    /* example usage */
    foreach($subfolders as $folder)
        echo '<option value=" '. $folder .' ">' . ucwords(strtolower(str_replace('_', ' ', $folder))) . '</option>';
    
    PHP:
     
    Danltn, Jan 3, 2009 IP