PHP遍历目录

PHP遍历目录有两种方式,一种是使用传统的opendir()方式,另外一种是使用DirectoryIterator方式。

使用opendir()遍历目录:

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
        }
        closedir($dh);
    }
}

使用DirectoryIterator遍历目录:

$files = new DirectoryIterator($path);
foreach ($files as $file) {
    $this->_storage->offsetSet($file->getFilename(), $file->getFileInfo());
}
原文地址:https://www.cnblogs.com/eastson/p/2837908.html