微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PHP中使用scandir获取过滤的文件名列表?

我想在文件夹中获取所有’_img’和PDF类型的文件

而不是使用

$fileArray = scandir($dir);
foreach ($fileArray as $file) {
    if (preg_match("_img",$file) && pathinfo($file,PATHINFO_EXTENSION) == 'pdf'){
        $filteredArray[] = $file;
    }
}

有没有捷径或者这是最好的方式?谢谢

解决方法

使用PHP glob()功能

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function,which is similar to the rules used by common shells

<?PHP 
  $filteredArray = array();
  foreach (glob($dir."/*_img.pdf") as $filename)
     $filteredArray[] = $filename;
?>

还有fnmatch()功能,它将文件名与模式匹配

最后一个解决方案是使用DirectoryIteratorFilterIterator

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐