Skip to content
PHP Logo

What are some methods in PHP to read all files in a specific directory and output them in a selection menu?

To read all files in a specific directory and output them in a selection menu in PHP, you can use the glob() function to retrieve an array of file names in the directory. Then, loop through the array and create an HTML select menu with the file names as options.

php
$directory = "path/to/directory/";
$files = glob($directory . "*");

echo "<select>";
foreach ($files as $file) {
    $fileName = basename($file);
    echo "<option value='$fileName'>$fileName</option>";
}
echo "</select>";