Are there any potential pitfalls in trying to read the directory structure into a string in PHP?

One potential pitfall in trying to read the directory structure into a string in PHP is that it may consume a large amount of memory if the directory contains a large number of files and subdirectories. To solve this issue, it is recommended to read the directory structure incrementally rather than loading it all into memory at once.

$directory = '/path/to/directory';
$files = scandir($directory);

foreach ($files as $file) {
    if ($file != '.' && $file != '..') {
        echo $file . PHP_EOL;
    }
}