现在的位置: 首页 > 综合 > 正文

PHP遍历目录

2012年02月22日 ⁄ 综合 ⁄ 共 379字 ⁄ 字号 评论关闭

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

抱歉!评论已关闭.