I'll have to ignore the MySQL bits cause I don't know MySQL.
There's a bit of waste/confusion with $counter and $total. Just use count($image) everywhere you need the # of images.
Let's use:
if ($handle = opendir($path . $current["id"] . "_" . $current["title"])) {
while (false !== ($file = readdir($handle))) {
if ($file!="." && $file!="..") {
$image[]=$file;
}
}
closedir($handle);
}
Also, you are using globals like $perpage instead of $_GET['perpage'] which makes the code a bit harder to follow.
I see a big problem!
$image is your big array of all the files in one directory. Yet you are replacing that array with the guts of a single image here:
$image = imagejpeg($path . $current["id"] . "_" . $current["title"] . "/" . $image[$a]...
You are trying to pass a wad of data, a JPG, thru an IMG SRC tag:
echo "<TD><IMG SRC=\"$image\" WIDTH=\"100\"></TD>\n";
IMG SRC is supposed to be the FILENAME of a JPG, not the actual file data. You have two choices:
Write the JPG somewhere on the server temporarily and img src="tempdir\image.jpg".
Rework your script so it can call itself like:
<img src="imagehandler.php?action=dumpjpeg&name=DSCF0032.jpg">, then it will return just the image data.