2009年1月19日星期一

SPL DirectoryIterator

DirectoryIterator 提供目录迭代功能。可替换 opendir() 和 dir 伪类功能。

opendir() 写法:
$path = '.';
if (is_dir($path)) {
if ($dh = opendir($path)) {
while (false !== ($file = readdir($dh))) {
echo "filename: $file \tfiletype: ", filetype($path . $file), "\n";
}
closedir($dh);
}
}


dir 伪类写法:
$path = '.';
$dir = dir($path);
while (false !== ($entry = $dir->read())) {
echo $entry, "\n";
}
$dir->close();


主角出场:
$path = '.';
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
echo 'filename:', $fileinfo->getFilename(), "\tfiletype:", $fileinfo->getType(), "\n";
}


参考资料:
http://www.php.net/manual/en/class.directoryiterator.php
http://www.php.net/manual/en/class.splfileinfo.php
http://www.php.net/~helly/php/ext/spl/

没有评论: