Show Files in Directory With PHP

PHP scandir() Function

The scandir() function can be used to generate a list of filenames from a chosen folder. The following code demonstrates how to generate such a list.

First of all, a directory is chosen by creating the $dir variable.

Secondly, the $files1 array is created by obtaining all images with the scandir($dir) function.

Finally, a foreach loop is created to output a check box and images for each file in the array.

$dir =  getcwd()."/images/gallery" ;

$files1 = scandir($dir);

foreach($files1 as $file){
if(strlen($file) >=3){
//$foil = strstr($file, 'jpg'); // As of PHP 5.3.0
$foil = $file;
//$pos = strpos($file, 'css');
if ($foil==true){
echo '<input type="checkbox" name="filenames[]" value="'.$foil.'" />';
// echo "<img width='130' height='38' src='images/gallery/$file' /><br/>"; // for live host
echo "<img width='130' height='38' src='/ABOOK/SORTING/gallery-dynamic/images/gallery/$file' /><br/>";