What are the advantages and disadvantages of storing an image as a blob in the database vs storing just the file name in the database.
I'm using PHP(CodeIgniter) with MySQL.
I know this question is subjective but a client asked me th开发者_开发技巧is question and I couldn't give a good answer.
I'd generally say that :
- database is harder to scale :
- having a DB that has a size of several GB because of images will make many things (backup, for example) harder.
- do you want to put more load on your DB servers, to serve... files ?
- serving images from the filesystem is something that webservers do well
- and it allows one to do some tasks (like generating thumbnails, for example) without involving the DB server
- I suppose, if needed, that using plain old files will also make things easier the day you want to use some kind of CDN
Which means I would almost always store images / files in the filesystem, and only store the file's name (and, possibly, size and content-type -- that can always be useful) in the database.
Thinking about it, that question is probably a duplicate... Oh, yes, it actually is ; see at least those questions+answers :
- Store images(jpg,gif,png) in filesystem or DB?
- Storing Images : DB or File System -
- Storing Images in DB - Yea or Nay?
- store image in database or in a system file ?
I doubt your client have enough experience to judge an answer.
I was asked once by customer to store image strictly into database. After some investigation I've discovered that they just meant "to store images on the server side", because they had an MS Access database before and it has a choice between storing images in the database or just links to the images, while image itself stayed at the users hard disk and often been unavailable.
As for your question, I doubt that image has any relation with relational model. You cannot sort, filter, relate with other rows based on blob content. While you have to do many things to support images in the database - dedicated script to show, emulate conditional get, etc. I know, this feature exists. But it's more an obvious attempt to follow the fashion than a real need, especially in the web development, where images being requested with separate requests.
I prefer to rename images files when uploaded and store them into db row related to his own parent (user,product,) ... then upload them into 3 folder:
/uploads
/img
/products
/small
/medium
/xxl
resizing them into small (50x50) , medium (90x90), xxl (original dimension), before moving them into these directories.
if you need SEO images you can store them for example:
id | product | image
1 book 1-book.png
so you'll get id and product name in same image file.
or you can even just store
id | product | image
1 book 1.png
then is simple to attach src paths :
/*Config file*/
$config['base_static_products_url'] = 'uploads/img/products/';
/*View file*/
<img src="<?php echo base_url().$this->config->config['base_static_products_url'].'/small/'.$row->image ?>" alt=""/>
OR (no SEO)
<img src="<?php echo base_url().$this->config->config['base_static_products_url'].'/small/'.$row->id.'.png' ?>" alt=""/>
精彩评论