Forum Moderators: coopster

Message Too Old, No Replies

Uploading and downloading images from database

There in but not coming out at the minute :-s

         

Jamier101

5:24 pm on Sep 28, 2010 (gmt 0)

10+ Year Member



Since my site is going to be holding quite a large amount of images the school of thought with the IT department at work was to upload the images into a database much like we do at work, with this in mind and a pointer in the right direction I've been able to upload the images into my database but seem to be having a hard time drawing them back on on demand :-S

Does anyone have any experience of doing this?

enigma1

8:39 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have options like with regular files, the one to use the traditional way and setup the url for the image that points to a script and echos the header and image content you pull from the db.

So the html you sent to the client will be like you have a file but points say to a php script

<img src="http://example.com/image.php?img_id=15" />

then in the image.php script

<?php
// Query db and fetch
$q = mysql_query("select image_data from images where id='" . (int)$_GET[img_id] . "'");

if( !mysql_num_rows($q) ) exit();

$image_array = mysql_fetch_array($q);
header ('Content-type: image/png');
echo $image_array['image_data'];
?>

the other is with embedded images and html

<img src="data:image/gif;base64,..<db image data>.....

penders

8:51 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you send the data/image back to the browser, are you sending the appropriate Content-Type header?

As an aside... I assume you have your reasons, but if you have a large amount of images and you are serving these to a browser then I'm not convinced that the best approach is to necessarily store these in the DB, as opposed to the file system?