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!


<select name="menu">
<option value="">MSN</option>
<option value="">Google</option>
<input type="button" onClick="[].value;" value="GO">

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:

      $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) ?>
    <?php } ?>

    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.

    点赞 评论