douyun8885
douyun8885
2012-05-30 20:07
浏览 37
已采纳

下拉菜单列表文件夹

I need to make a dropdown menu for a website i'm working on and in that dropdown i would like to list all files in a directory but due to my lack of web development knowledge i'm struggling a little to be able to figure it out.

Any help would be greatly appreciated!

EDIT

<form>
<select name="menu">
<option value="http://www.msn.com/">MSN</option>
<option value="http://www.google.com/">Google</option>
</select>
<input type="button" onClick="location=this.form.menu.options[this.form.menu.selectedIndex].value;" value="GO">
</form>

That's about the extent to which i know how to make a dropdown menu

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanjing4667
    duanjing4667 2012-05-30 20:16
    已采纳

    Look at PHP's scandir function. Loop through the result and echo your HTML code, one <option> tag per loop iteration.

    Something like this:

    echo "<select name='files'>";
    $files = array_map("htmlspecialchars", scandir("path/to/your/files"));
    foreach ($files as $file)
        echo "<option value='$file'>$file</option>";
    echo "</select>";
    
    点赞 评论
  • dongzhazhuo0572
    dongzhazhuo0572 2012-05-30 20:18

    You could do something like this:

    <select>
    <?php
      $source_dir = '/var/www/whatever';
      $dir_handle = opendir($source_dir);
      foreach (glob('*', GLOB_ONLYDIR) as $dir_name) {
    ?>
      <option value="<?= htmlentities($dir_name) ?>">
        <?= htmlentities($dir_name) ?>
      </option>
    <?php } ?>
    </select>
    

    If you want to sort, filter, or whatever, just manipulate the array returned by glob() however you want. Alternatively, you can use readdir() calls followed by is_dir() calls. There are a bunch of different ways to do it, it just boils down to your specific need.

    点赞 评论

相关推荐