Well one problem I see is with the line...
CODE
// This is the old line
<img src="download2.php?id=<?php echo $id ?>" alt="Images" />
// This is what it should be
<img src="download2.php?id=<?php echo "$id"; ?>" alt="Images" />
Notice we wrapped the $id in double quotes so it is evaluated and then make sure to end with a semicolon.
After making this change, view the page source and see if indeed the links are correctly showing the ids from your database. We want to make sure that your links are first formatted correctly.
Next I am not sure if download2.php is another file or if you are attempting to call itself. I would probably recommend that you move all your header functions and readfile into another file and call it from the first file.
So your second file should look like this...
php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'library/config.php';
include 'library/opendb.php';
$id = $_GET['id'];
$query = "SELECT name, type, size, path FROM upload2 WHERE id = '$id' ";
$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);
// Notice here we use basename to get just the file name, not entire path
header('Content-Disposition: attachment; filename='.basename($filePath));
// Then we use the filesize to get the actual size of our file (using the full path)
header('Content-Length: ' . filesize($filePath));
// Here we use a content type of application/octet-stream because that is what images are
header('Content-Type: application/octet-stream');
// Here are some parts you were missing, one to clean the buffer and the other to flush it out.
ob_clean();
flush();
// Then read our file out to output
readfile($filePath);
include 'library/closedb.php';
exit;
}
Now that you got that part setup, you should be able to pass it ids and it will serve you up the files as is using a popup download window.
Going back to your links... you attempt to call this file downloader through the img src... this would work if PHP was writing to the output and it was not setup as an attachment style like you had in your code. But if you want this to now work as we have it here, you would simply create a HTML link which would then call download2.php and pass it the id and suddenly it would present the image download popup. As for displaying the image on the first page, you can use the img tag with the fullpath from the database to show the file and the other variables as alt tag information or part of the link text.
It is up to you. Hopefully this gives you some ideas of how this can be done and how you can make an image gallery style script where then people could click images and download them.
Enjoy!
"At DIC we be image downloading code ninjas... we also upload naked pictures of ourselves because we are sexyyyyyy"