我想要一个特定文件夹中的图像文件列表。 我试过这个,但不适用于所有文件:
$files = scandir("/folder/");
foreach($files as $file) {
if (substr($string, -4) == '.jpg'){
$f[] = $file;
}
什么是正确的方法?
答案 0 :(得分:2)
首先获取所有文件,但省略以“。”开头的文件。 然后用一个简单的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);