PDA

View Full Version : Stroring images in MySQL



KazanDaemon
03-08-2009, 05:41 PM
Is it possible to store user-posted images (max 50 KB) in MySQL database?
For example, user registering at my forum and uploading his avatar image. I would like to store it in MySQL, not as image file on disk. I think it will more comfortable, because there will be never problem with giving name to userfile and synchronization database with files after restoring backups.
But is it real? Did someone store images in database before? Some images become incorrect when converting to text format. Is it available to store binary data in MySQL? And how to realize it?

felgall
03-09-2009, 12:42 AM
You could base64 encode the image before storing it - that converts it so that it can be stored as text (although it will then take up 4 times as much space in the database as it would as a separate file).

Alternatively you may just be able to use a BLOB field to store it without any conversion being required.

wysiwyg
03-09-2009, 04:34 AM
Yes, you can store binary data as a blob. No, you shouldn't, especially not when you have limited resources. You'll take a substantial performance hit.

Files should be stored on the file system, and information about them should be stored in the database.

If you must, there are plenty of tutorials out there that go into specifics.