导航

PHP遍历目录

Posted on 2012-12-28 21:30  eastson  阅读(320)  评论(0编辑  收藏  举报

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());
}