Page 1 of 1
Displaying images from MySQL database
Posted: Mon May 23, 2005 6:06 pm
by dylan001
I'm trying to display images stored in a MySQL database (I can't avoid it!!!) They are formatted as a "BLOB", which from my understanding is the binary format of the picture.
I'm pretty new to php and don't know how to properly call and display an image from the database.
I've been able to call and display the other text coloums.
I'm guessing it's because I need to convert the binary back to the a relevant picture format somewhere along the line???
Any ideas, advice, tutorials???
Sorry if this is listed somewhere else in the forum I could find it anywhere.
Thanks.
Posted: Mon May 23, 2005 6:40 pm
by Burrito
you need to send header information down to the client about the type of information you're going to send ie: Content-Type:, Content-Length:, Content-Disposition:, etc...then just echo the data from your blob field below the header information.
see below snippet from a similar project I did a while back:
Code: Select all
// Send down the header to the client
Header ( "Content-Type: {$fileinfo['attachmenttype']}" );
if (!empty($fileinfo['attachmentsize']) && $fileinfo['attachmentsize'] > 0)
Header ( "Content-Length: {$fileinfo['attachmentsize']}" );
if (!empty($fileinfo['attachmentfilename']) && substr($fileinfo['attachmenttype'], 0, 5) != 'image')
Header ( "Content-Disposition: attachment; filename={$fileinfo['attachmentfilename']}" );
// Loop thru and stream the nodes 1 by 1
for ($i = 0; $i < count($attachparts); $i++) {
$sql = "select filedata from ttticketattachmentsdata where id = " . $attachparts[$i];
$handle = mysql_query($sql)
or trigger_error("Couldn't get file part", E_USER_ERROR);
$data = mysql_fetch_row($handle);
echo $data[0];
mysql_free_result($handle);
}
}
Posted: Mon May 23, 2005 6:44 pm
by John Cartwright
I'm trying to display images stored in a MySQL database (I can't avoid it!!!)
Why not?
Posted: Mon May 23, 2005 6:45 pm
by dylan001
That looks like exactly what i'm after!!!
Thanks for your help
Posted: Mon May 23, 2005 7:35 pm
by dylan001
Quote:
I'm trying to display images stored in a MySQL database (I can't avoid it!!!)
Why not?
well its obviously a lot easier to upload the files to the server and simply link to them, but it won't be me doing this, and the person the site is intended for won't be able to do this.
Hence a small script to create a simple web form for them to upload thier images is much easier, and hence the storage in the database.
Posted: Mon May 23, 2005 7:54 pm
by John Cartwright
Only reason I'm saying this is because storing images in your database can slow down you site quite a bit at times. If you don't mind me asking again.. why can't this user store files? Permission problems?
Posted: Mon May 23, 2005 8:18 pm
by dylan001
Yeah, i know bout the speed problems, but i'd didn't think it would make a huge difference, its only going to be a low traffic site anyway.
I'm using the database cause its the best way I can script a solution for someone with next to no internet experience to upload their photos to a gallery site im designing.
I'm pretty new to php, so i don't know if there is an easy way to create a simple web form that will allow upload to my server (well not mine - and so i guess there may be permission restrictions) rather than the database?
Thanks for the advice.
Posted: Mon May 23, 2005 8:22 pm
by Burrito
well to the end user, it should be sixes either way. the only difference will be on the back end (your part).
the only time you should insert files to a DB is if you can not (usually for permissions issues) save them to the HDD.
I gotta concur with JCart...do your absolute best to force the issue of using the filesystem vs the db.
Posted: Mon May 23, 2005 8:33 pm
by dylan001
I gotta concur with JCart...do your absolute best to force the issue of using the filesystem vs the db.
Do you guys have any pointers on how I would go about uploading the files to the server if I didn't have any permission errors???
Thanks for the advice.
Posted: Mon May 23, 2005 8:40 pm
by Burrito
study up on the
$_FILES[] array then use the
move_uploaded_file() function.
it's pretty simple really, but let me know if you have any questions.
Posted: Mon May 23, 2005 8:43 pm
by dylan001
Thanks for the advice, i'll get onto that. It might take me a while but should get there eventually.
Thanks once again for your help, it's appreciated!
Posted: Mon May 23, 2005 8:43 pm
by John Cartwright
Posted: Mon May 23, 2005 8:44 pm
by Burrito
ha! beat you to it JCart
