Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can i insert an image in a MYSQL DB table?

is there a way to insert pics(not url,the pic)into a MYSQL table made with phpmyadmin? and if there is when i want to get that picture and insert it in the page , what should i do? :)

like image 782
kasrsf Avatar asked May 18 '10 14:05

kasrsf


2 Answers

It is possible (using the BLOB type) but it is almost always better to just store the image in the filesystem and just store the path to the image in the database.

A few reasons why off the top of my head:

  1. BLOBs will balloon the size of your database, making backups and restores potentially more difficult (although some might argue that it makes it easier since you only have to back up the database rather than the database plus a bunch of files).

  2. Storing the data as BLOBs could cause performance issues if you have lazy code that uses select *.

  3. Serving the images from the filesystem will be faster than serving them from the database, and reduce load on the db server.

  4. You get some flexibility with where you serve images - for example if you want to move them to a CDN in the future.

like image 111
Eric Petroelje Avatar answered Sep 30 '22 03:09

Eric Petroelje


When it comes to static resources such as background images or menu icons etc, I've never seen anyone store them in the database. For dynamic content such as user uploaded images, it's not that uncommon.

Here are some pro's and con's of storing images in the database.

Benefits:

  • It's technically no different from other data. Just more, and in binary format.
  • Images are backed up along with the rest of the database.
  • No head ache of getting out of sync (having a path in the database pointing to a non-existent image, or unreferenced images in the database)

Drawbacks:

  • Storing individual files is precisely what the file system is geared towards, so it will most likely scale better.
  • Easier to manually debug / view the images
  • Web server can serve the images directly (no need for extra code to load content from database and serve proper mime type etc).

Personally I've always found that the benefits outweighs the drawbacks and always had the images (along with mime-types) in the database.

Related questions and resources:

  • How can I store and retrieve images from a MySQL database using PHP?
  • Quora: Is it a bad design to store images in database?
  • Video tutorial: PHP/MySQL - Save/Retrieve image from database
like image 37
aioobe Avatar answered Sep 30 '22 04:09

aioobe