images in database....

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

images in database....

Post by dull1554 »

how would one go about storing a image in a sql database.....that is i want the image stored in the database instead of on my server....do you catch my drift....is it possiable if so how would it be done....????!!!!

thanks a mill.......

(i saw someting about it on google and could not find a tutorial to see how its done.....and checked the php man...but with no luck)
User avatar
xisle
Forum Contributor
Posts: 249
Joined: Wed Jun 25, 2003 1:53 pm

Post by xisle »

a blob is used for storing binary data...
http://www.mysql.com/doc/en/BLOB.html
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You need to create a BLOB (or LONGBLOB) column in your database, read the image file into a $var (using fread() for example) and then chuck it into your database in the usual way.

To read it you will need to load the image string from the database (in the usual way) and then use something like header("Content-Type: image/jpeg"); echo $imageBlob;

It's not too difficult to get sorted but the general rule of thumb is not to store images in a database, just keep the images in a folder and then just add the path name of the image to the database. It doesn't matter if your images are in a database or in a folder they still take up the same amount of server space, and image loading from a database tends to be a lot slower than loading them from a folder.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

ahhhh, see i did not know that, thanks for the help....maybe ill just keep them in a folder with a link to the image in the database....thanks a bunch
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Another method I've heard of (but never tired personally), is using the encode64() function to convert the image to a 64bit encoded string. Then, when you want to display the image, just use decode64() on the string, and dump it to the browser. File extensions will not be preserved, but from what I've heard, the file headers will be, so the browser will display the image properly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply