Are there any potential performance issues to consider when using readdir() and substr() functions in PHP to filter files in a directory?

When using readdir() and substr() functions in PHP to filter files in a directory, potential performance issues may arise if the directory contains a large number of files. This is because readdir() reads the entire directory contents into memory, which can be inefficient for large directories. To solve this issue, it is recommended to use the glob() function with a specific pattern to filter files directly without reading the entire directory contents.

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

foreach($files as $file) {
    echo $file . "\n";
}