I am writing some code that posts images onto a page in NUMERICAL order. The images are numbered 1.jpg, 2.jpg, 3.jpg... ect. I need it to check if the image exists (in case it has been deleted) and if not post the next image. It works now but does not work for big gaps in numbers. If image 3, 7, and 8 are missing it will post all the images. But if image 3 through 15 are missing... it only posts the first 3. Logical error?? Here is the code.
<?php
function createGallery( $pathToImages, $pathToThumbs )
{
echo "<table cellspacing=\"0\" cellpadding=\"3\" width=\"500\">";
echo "<tr>";
// open the directory
$dir = opendir( $pathToImages );
$counter = 0;
$image_counter = 1;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
if (!file_exists("{$pathToImages}$image_counter.jpg"))
{
$counter += 1;
$image_counter += 1;
}
if (file_exists("{$pathToImages}$image_counter.jpg")){
echo "<td ><center><a href=\"{$pathToImages}$image_counter.jpg\">";
echo "<img src=\"{$pathToThumbs}thumb_$image_counter.jpg\" border=\"0\" />";
echo "</a><BR><CENTER>$image_counter</td>";
$counter += 1;
$image_counter += 1;
if ( $counter % 5 == 0 ) { echo "</tr><tr>"; }
}//if file exists
}
// close the directory
closedir( $dir );
echo "</tr>";
echo "</table>";
}
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed.
createGallery("pictures/","thumbs/");
?>
Any help is great. Thanks