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
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>";
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.