我只想在某个文件夹中仅列出图像文件。我试过了,但是不适用于所有文件:
$files = scandir("/folder/");
foreach($files as $file) {
if (substr($string, -4) == '.jpg'){
$f[] = $file;
}
正确的方法是什么?
首先获取所有文件,但忽略以“。”开头的文件。然后一个简单的preg_match来获取所有图像文件
$files = array_diff( scandir("/folder/"), array(".", "..") );
$images = array()
for ($file in $file){
if (preg_match("/\.(jpg|jpeg|png)$/", $file)){
$images[] = $file;
}
}
或者
$images = glob("/folder/*.{jpg,gif,png,jpeg}", GLOB_BRACE);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句