Mixstah
Mixstah

Reputation: 411

populate a 'select list' from a directory on my server using php and html

Is it possible (I am assuming not) to populate a drop down list from files that are on my website, say in the images folder? in a html form?

<select name="s1">
      <option value="" selected="selected">-----</option>
  <?php 
       foreach(glob('/images/*') as $filename){
       $rest = substr($filename, 7);    
       echo "<li><a href='#'>".$rest."</a></li>";
    }
?>

</select> 

Upvotes: 1

Views: 11165

Answers (2)

sroes
sroes

Reputation: 15053

Try it like this:

<select name="s1">
      <option value="" selected="selected">-----</option>
  <?php 
       foreach(glob(dirname(__FILE__) . '/images/*') as $filename){
       $filename = basename($filename);
       echo "<option value='" . $filename . "'>".$filename."</option>";
    }
?>

</select> 

Upvotes: 4

Cezary Wojcik
Cezary Wojcik

Reputation: 21845

You can use scandir - http://php.net/manual/en/function.scandir.php

Then just run a foreach on the returned array and echo <option> for each one.

Upvotes: 5

Related Questions