What are the potential pitfalls of using readdir() instead of glob() in PHP for listing directories?

Using readdir() instead of glob() in PHP for listing directories can be more error-prone and require more manual handling. readdir() requires more code to handle reading the directory entries, checking for the end of the directory, and filtering out unwanted entries. On the other hand, glob() simplifies this process by returning an array of matching file paths directly. To solve this issue, you can use glob() instead of readdir() to list directories in PHP. This will simplify the code and make it easier to work with directory listings.

// Using glob() to list directories
$directories = glob('/path/to/directory/*', GLOB_ONLYDIR);
foreach ($directories as $directory) {
    echo $directory . PHP_EOL;
}