Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php:Store image into Mysql blob, Good or bad?

Tags:

php

mysql

blob

this question is confusing me so i thought i should listen to an expert voice !.

is it better to upload images to a folder and just save link to mysql, or better upload img itself into a blob mysql field ?

thank you very much

like image 747
Zalaboza Avatar asked Jun 11 '11 04:06

Zalaboza


2 Answers

I have often built systems to store images in the database, there are pros and cons to doing this.

Pros:

  • All your data is kept in one place, if you migrate your website/database the images will just be there
  • Its easier to sort/delete/etc...
  • Since you have to serve it via a PHP script, you can perform additional things such as security if required, or image processing (obviously you can do this with flat file too, but you have to make sure the security cant be bypassed by leaving the images in a public directory).

Cons:

  • Its slower then serving a flat file from the webserver as a PHP script needs to retrieve it, and MySQL needs to return the data.
  • Your database will become large very fast and not all web hosts take too kindly to this.
  • The file system is faster for flat file storage and retrieval as thats exactly what a file system is designed for.
like image 105
Geoffrey Avatar answered Sep 28 '22 08:09

Geoffrey


Bad. Your webserver does a much better job managing expiry headers and directly loading files from the filesystem. Throughput will be much higher using the filesystem. It's what it's designed for, utilize it.

SQL databases are designed for relational data, not images. You're just loading your database unnecessarily. Store the path/image name instead.

like image 42
Xorlev Avatar answered Sep 28 '22 09:09

Xorlev