Listing all images in a directory using PHP

Use glob function.

<?php
define('IMAGEPATH', 'images/');
foreach(glob(IMAGEPATH.'*') as $filename){
    $imag[] =  basename($filename);
}
print_r($imag);
?>

You got all images in array format


I like PHP's glob function.

foreach(glob(IMAGEPATH.'*') as $filename){
    echo basename($filename) . "\n";
}

glob() is case sensitive and the wildcard * will return all files, so I specified the extension here so you don't have to do the filtering work

$d = 'path/to/images/';
foreach(glob($d.'*.{jpg,JPG,jpeg,JPEG,png,PNG}',GLOB_BRACE) as $file){
    $imag[] =  basename($file);
}

Tags:

Php

Image