How can PHP be used to automatically display all subfolders in a directory in a dropdown menu on a webpage?
To automatically display all subfolders in a directory in a dropdown menu on a webpage using PHP, you can use the `scandir()` function to retrieve a list of all directories within the specified directory. Then, iterate through the list and generate HTML `<option>` tags for each subfolder to populate the dropdown menu.
<select>
<?php
$directory = "path/to/directory";
$subfolders = array_diff(scandir($directory), array('..', '.'));
foreach ($subfolders as $subfolder) {
if (is_dir($directory . '/' . $subfolder)) {
echo "<option value='$subfolder'>$subfolder</option>";
}
}
?>
</select>
Keywords
Related Questions
- What is the purpose of creating an array from a database in PHP?
- How can the PHP script be modified to display the day of the week for a specific date stored in a variable ($datum)?
- How can relative path errors in the action attribute of a form tag impact the functionality of inserting form data into a MySQL database using PHP?