简体   繁体   English

使用php和html从服务器上的目录填充“选择列表”

[英]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? 是否可以(我假设不是)从网站上的文件(例如images文件夹)填充下拉列表? in a html form? 以html格式?

<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> 

You can use scandir - http://php.net/manual/en/function.scandir.php 您可以使用scandir - http: scandir

Then just run a foreach on the returned array and echo <option> for each one. 然后,只需在返回的数组上运行一个foreach ,然后对每个数组执行<option>

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM