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

2019-01-07 21:28发布

问题:

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

回答1:

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.


回答2:

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.



回答3:

If your application is large i.e you have to display a large number/size of images repeatedly then you should go for first method (storing only image path in database and actual images on file system). This will reduce the processing time to display images moreover consumes less resources. Secondly, if your application requires less number of images then you can store them directly in database . This way it becomes easy to take backups and port application to another OS.



标签: php mysql blob