Using opendir() output to populate a drop down form box

Discussion in 'PHP' started by Colbyt, Dec 12, 2008.

  1. #1
    I am currently using this script fragment

    //using the opendir function to list the files in the directory
    $dir_handle = opendir("./$localfolder") or die("Unable to open Directory");
    	//echo "$dir_handle";
    
    //running the while loop
    while (false !== ($file = readdir($dir_handle))) 
    {  
    //if($file!="." && $file!="..")   //backup
    if($file!="." && $file!=".." && $file!="index.php")
    {    
     $name=$file; 
     echo $name;
     echo "<br>";
    }
    }
    Code (markup):
    to read the contents of a directory. I would like to populate a dropdown box on an html form with those results so that one item can be selected and submitted.

    I am assuming that I will need to array them out and then get it to selection portion. Forms are my weakest point.

    Really could use a little help.
     
    Colbyt, Dec 12, 2008 IP
  2. GreatMetro

    GreatMetro Peon

    Messages:
    117
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    //using the opendir function to list the files in the directory
    $localfolder = "";
    $dir_handle = opendir("./$localfolder") or die("Unable to open Directory");
    //echo "$dir_handle";

    //running the while loop
    while (false !== ($file = readdir($dir_handle)))
    {
    //if($file!="." && $file!="..") //backup
    if($file!="." && $file!=".." && $file!="index.php")
    {
    $name[]=$file;
    }
    }
    ?>

    <select name="fileName">
    <?php foreach ($name as $filename) { ?>
    <option value="<?=$filename;?>"><?=$filename;?></option>
    <?php } ?>
    </select>
     
    GreatMetro, Dec 12, 2008 IP
    Colbyt likes this.
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    Looks like it might work. Let me play with that.

    +rep for the effort whether it works or not.
     
    Colbyt, Dec 12, 2008 IP