What are the potential pitfalls of using glob() function in PHP to select specific files based on their names?

One potential pitfall of using the glob() function in PHP to select specific files based on their names is that it may not handle special characters or patterns in file names correctly. To avoid this issue, you can use the fnmatch() function within a loop to filter files based on specific patterns or regular expressions.

$files = glob('path/to/files/*');

foreach ($files as $file) {
    if (fnmatch('pattern*', basename($file))) {
        // Process the file
    }
}