What are some potential pitfalls when dynamically creating a menu in PHP based on directory structure?

One potential pitfall when dynamically creating a menu in PHP based on directory structure is that the menu may include files or directories that should not be displayed to users. To solve this issue, you can filter out unwanted files or directories by checking their names or paths before adding them to the menu.

<?php
// Define the base directory
$baseDir = 'path/to/directory';

// Get all files and directories in the base directory
$items = scandir($baseDir);

// Filter out unwanted files or directories
$filteredItems = array_filter($items, function($item) {
    return !in_array($item, ['.', '..', 'file_to_exclude.php', 'directory_to_exclude']);
});

// Create the menu based on the filtered items
echo '<ul>';
foreach ($filteredItems as $item) {
    echo '<li><a href="' . $item . '">' . $item . '</a></li>';
}
echo '</ul>';
?>