Is there a preferred alternative to readdir for reading directories in PHP?

The preferred alternative to readdir for reading directories in PHP is to use the DirectoryIterator class. DirectoryIterator provides an object-oriented way to iterate over the contents of a directory, making it easier to work with files and directories.

$directory = new DirectoryIterator('/path/to/directory');

foreach ($directory as $fileInfo) {
    if (!$fileInfo->isDot()) {
        echo $fileInfo->getFilename() . "\n";
    }
}