0

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>
Iain Simpson
  • 8,011
  • 13
  • 47
  • 66

3 Answers3

2

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

Raab
  • 34,778
  • 4
  • 50
  • 65
0

Your variable $thelist is only known in your while-loop.

If you change your beginning to this, it should work:

<?php
$thelist = "";
if ($handle = opendir('images/photobanner')) {

The technical term for your problem is 'scope'.

Snicksie
  • 1,987
  • 17
  • 27
0

Maybe glob() is best for you, and you have some extra ':

$thelist = '';
foreach (glob("images/photobanner/*.jpg") as $file) {
    $thelist .= '<li><a href="'.$file.'"><img src="'.$file.'"/></a></li>';
}
echo $thelist;
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107