What is the best place for storing uploaded images, SQL database or disk file system?
I generally store files on the file-system, since that's what its there for, though there are exceptions. For files, the file-system is the most flexible and performant solution (usually).
There are a few problems with storing files on a database - files are generally much larger than your average row - result-sets containing many large files will consume a lot of memory. Also, if you use a storage engine that employs table-locks for writes (ISAM for example), your files table might be locked often depending on the size / rate of files you are storing there.
Regarding security - I usually store the files in a directory that is outside of the document root (not accessible through an http request) and serve them through a script that checks for the proper authorization first.
The only benefit for the option B is having all the data in one system, yet it's a false benefit! You may argue that your code is also a form of data, and therefore also can be stored in database - how would you like it?
Unless you have some unique case:
- Business logic belongs in code.
- Structured data belongs in database (relational or non-relational).
- Bulk data belongs in storage (filesystem or other).
It is not necessary to use filesystem to keep files. Instead you may use cloud storage (such as Amazon S3) or Infrastructure-as-a-service on top of it (such as Uploadcare):
https://uploadcare.com/upload-api-cloud-storage-and-cdn/
But storing files in the database is a bad idea.
I know this is an old post. But many visitors to this page are getting nothing related to the question. Especially for a newbie.
How to upload and store images or file in our website:
For a static website there maybe no problem since the file storage for some share hosting still adequate. The problem comes from a dynamic website when it gets bigger. Bigger in the database can be handled, but bigger in file such as images is becomes a problem. There are two type of images in a website:
Images come from the administrator for dynamic blog. Usually, these images have been optimized before upload.
Images from users in case of users is allowed to upload images such as avatar. Or users can create blog content and put some images from text editor. This kind of images is difficult to predict the size. Users can upload big images just for small content by resize the view size but not resize the image size.
By ignoring item no. 1 above, quick solution for item no. 2 can be temporary solved by the following tips if we don't have image optimizer functionality in our website :
Do not allow users to directly upload from text editor by redirecting them to image gallery. On this page users must upload file in advance before they can embedded in the content. This method is called as a File Manager.
Use a crop image function for users to upload images. This will limit the image size even users upload very big file. The final image is the result of the cropped image. We can define the size in server side and accept only for example 500Kb or lower.
Now, that is only temporary. For final solution, the question is repeated :
- How to handle a big images storage?
- Resize or change the extension.
- How a big or medium website or e-commerce handle the file storage for their images?
What we can do then :
Migrate from share hosting VPS. Not enough? Then more higher by upgrading to Dedicated.
Create your own server for file storage. Googling to do it. This is not as difficult as you think. Some people do it for their website.
The easy way is use the CDN file storage service.
Okay, 1 and 2 is little bit expensive. But no 3 I think is the best solution.
Some CDN services allow you to store as many web files as you want.
Question, "how to upload file to CDN from our website?"
Don't worry, once you register, usually free, you will get guidance how to upload file and get their link from/to your website. You will get an API and more. It's easy.
Some providers give us a free service for 14 days with limited storage and bandwidth. But that will be okay for starting point. The only problem is because 'people never try'.
Hope it will help for newbie.