致命错误:找不到类Filesystem迭代器


Fatal error: Class Filesystemiterator not found

我在尝试使用此代码时遇到错误

<?php
$fi = new FilesystemIterator("image/Images", FilesystemIterator::SKIP_DOTS);
$count = iterator_count($fi);
if ($count-2<0){
$i = 0;
}
else{
$i = $count -2;
}
echo $i;
?>

除了我写的,还有别的选择吗?我认为这可能与我的网络主机上的php版本有关。。。不过我不想更改版本,所以变通一下会很有帮助。

只需像这样使用glob()count()

$fi = glob("image/Images/*.*");
$count = count($fi);

正如@Mark Baker在评论-中提到的那样

FileSystemIterator需要PHP>=5.3.0

我的PHP版本低于5.3.0,我升级了它,解决了我的问题。

在调用FileSystemIterator 之前,您可以检查phpversionclass_exists