phpimagereaddirclosedir

List all images in a directory not listing contents


I am using the following to list all of the jpg images in a directory, for some reason its not working though, I think some of my 's and .s may be in the wrong place, I think I have confused myself a bit :-S .

<?php
if ($handle = opendir('images/photobanner')) {
while (false !== ($file = readdir($handle)))
{
    if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'jpg')
    {
        $thelist .= '<li><a href="'.$file.'">'<img src="'.$file.'.'"/>'</a></li>';
    }
}
closedir($handle);
}
?>

<P>List of files:</p>
<UL>
<P><?=$thelist?></p>
</UL>

Solution

  • change the concatenating lins as below.

        $thelist .= '<li><a href="'.$file.'"><img src="'.$file.'"/></a></li>';
    

    and befor starting loop declare $thelist, that it is available in the scope