I am building a website where users will be allowed to upload images . There is also restriction on maximum amount of space each user can use .
I have two ideas in mind .
Which among the above is better? and Why?
Store the images as a file in the file system and create a record in a table with the exact path to that image. Or, store the image itself in a table using an "image" or "binary data" data type of the database server.
Storing images in a database table is not recommended. There are too many disadvantages to this approach. Storing the image data in the table requires the database server to process and traffic huge amounts of data that could be better spent on processing it is best suited to.
Answers. Hi, These two ways are both ok. My suggestion is to store images into sql database if there are no much more images, otherwise, it's better store them into file system because you can store them no matter how many there are.
sigh why does everybody jump to GridFS?
Depending on the size of the images and the exact use case, I'd recommend to store the images directly in the DB (not via GridFS). Here's why:
File System
af/2c/af2c2ab3852df91.jpg
, where the folders af
and 2c
are inferred from the file name (which itself might be a hash of the content for deduplication purposes).GridFS
GridFS is made for storing large files, and for storing files in a very similar way to a file system. That comes with some disadvantages:
fs.file
and one fs.chunk
document. Chunking is totally required for large files, but if your files are below 256k on average, there's no real chunking going on (default chunk size is 256k). So when storing small files in GridFS, you get the overhead without the advantage. Bad deal. It also requires two queries instead of one.Things might look different if you're operating a site for photographers where they can upload their RAW files or large JPEGs at 10MB. In that case, GridFS is probably a good choice. For storing user images, thumbnails, etc., I'd simply throw the image in its own document flat.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With