Вот одна строка, основанная на другой ответ на похожий вопрос:
// this will get you full path to images file.
$data = glob("path/to/images/*.{jpg,gif,png,bmp}", GLOB_BRACE);
// this will get you only the filenames
$data= array_map('basename', $data);
Изначально я хотел использовать решение @ Imran , но mime_content_type
было недоступно, и сервер (на котором у меня нулевой контроль) использует старые версии Apache и Php.
Так что я немного изменил его для работы с расширением файла, и я даю его здесь.
$imgDir = "images_dir";
// make sure it's a directory
if (file_exists($imgDir)) {
// select the extensions you want to take into account
$image_ext = array(
'gif',
'png',
'jpg',
'jpeg'
);
foreach (scandir($imgDir) as $entry) {
if (! is_dir($entry)) { // no need to weed out '.' and '..'
if (in_array(
strtolower(pathinfo($entry, PATHINFO_EXTENSION)),
$image_ext)) {
// do something with the image file.
}
}
}
}
Код протестирован и работает.