We help IT Professionals succeed at work.

Output a database stored image

Neil_Bradley
Neil_Bradley asked
on
I have a MySQL table which receives uploaded images.
 id  (int)
title  (varchar)
ext  (varchar)
image_time  (timestamp)
data  (blob)
 
The upload process works fine however I am having some trouble showing these images. The code snippet shows where I am. Where I would like to be is to have my query show the uploaded images.
Thanks.
N

if 
(!mysql_connect($db_host, $db_user, $db_pwd)) die("Can't connect to database");	
if (!mysql_select_db($database))    
	die("Can't select database");
	?><h1>My image gallery</h1>
<h2>Uploaded images:</h2>
<form action="<?=$PHP_SELF?>" method="post">

<?php $result = mysql_query("SELECT id, title, ext, data, blob, FROM {$table} ORDER BY id DESC");if (mysql_num_rows($result) == 0) // table is empty    
echo '<ul><li>No images loaded</li></ul>';else{    

echo // output my pics   ???

Open in new window

Comment
Watch Question

It is not very good idea to keep images in database - upload them into file system, say to images/ directory and put only filenames in database. Then displaying them will be easy:
echo "<img src='images/$filename'>";
Neil_BradleyWeb UX/UI Developer

Author

Commented:
Then maybe I could change my question to "does anyone have a good script for uploading images to a directory and storing the uploaded file data in a database"?
The script is ultimately aimed at allowing client to upload PDFs
Cheers,
Shinesh PremrajanEngineering Manager
Commented:
Yes @jfromanski options is valid, in  this scenario, the blob you are retriving cant be directly printed in the page, since these are having different headers.

you need to create a seperate function to display the images. which need to be called in the IMG tag of html
<img src="<?php echo getimage(); ?>" >

hope this helps
Neil_BradleyWeb UX/UI Developer

Author

Commented:
You are both correct.
Shinugs solution worked however jfromanski's comments put me on a different path. Down which I have found the best solution. It is much easier to upload the image to a directory then save the data from that uploaded image into the database.
Thanks both.
N