0

I want to create an image gallery and obviously, it must have images in it.

Somehow, I've been wondering about what's better between storing the images in a directory and retrieve them one by one or store them in the database as a BLOB data?

Thank you people! Cheers!

I am willing to learn either of the methods so please enlighten me.

witherwind
  • 472
  • 3
  • 15
  • Hope this will help you http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – Vijayakumar Selvaraj Feb 21 '14 at 05:16
  • [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay) Also: [To Do or Not to Do: Store Images in a Database](http://stackoverflow.com/questions/815626/to-do-or-not-to-do-store-images-in-a-database) – Jonathon Reinhart Feb 21 '14 at 05:26
  • Is "maintaining the file link in the database" the same with storing the image as a BLOB data? – witherwind Feb 21 '14 at 05:27
  • @witherwind No, they mean storing the file path in the database. (Or some identifier that allows you to re-construct the image URL). In general, that is the recommended way to do it. Although certainly not the *only* way. – Jonathon Reinhart Feb 21 '14 at 05:30
  • Storing the image URL in DB, but still uploading the image in a directory? – witherwind Feb 21 '14 at 05:32
  • 1
    @witherwind Yes. Store the images as files, because the file system is more optimized for dealing with data of that size than the database. Also, other optimizations like `sendfile(2)` are possible when the images are on the filesystem. – Jonathon Reinhart Feb 21 '14 at 05:36
  • I've been asking the same question myself before, and decided to store image as file and its path in the database. Since then I think that I made the right choice. – Petar Sabev Feb 21 '14 at 05:17
  • I've heard rumors that php can create many overheads if you retrieve images from the database, It was also mentioned that it is better to store a url in the database. I believe if you search SO you will find a topic like this – s3nzoM Feb 21 '14 at 05:16
  • As per this discussion [Storing Images in DB - Yea or Nay?](http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay), many of them suggested to go with file system approach. I mean, store the images in server & maintain the file path link in database. Please read the linked article, you will get some idea for sure. – Vijayakumar Selvaraj Feb 21 '14 at 05:22

2 Answers2

0

This question has been debated for many years. Advocates will make strong cases for each. Neither side as ever been definitively proven to be right in all cases.

Both methods break down when the number of images that you need to warehouse gets very large. Both databases and file systems have become better in the years since I bench marked both options against each other. At that time, you could fix the performance hit on the "file system" option by creating a hierarch of directories instead of putting them all in one directory. By now, file systems may have been optimized so that they don't choke when the number of directory entries gets large.

This is truly a "your mileage may vary" situation. Factors will include what file system will you use vs. what database engine will you use, how many images, what average size? Will you be "tagging" the images in the database as well as storing them?

Typically, you have to just try all the options until you find something that works in your configuration.

Definitely stress test it. If you think you need to store one million images, don't test with five and assume that it will scale. Test it with at least a million images, if not with two or five million.

That said, if you only need to store 1,000 images (or less) maybe even 10k or less and if you need to index the images by attributes like date, location, subject matter, etc. then at the risk of offending many well meaning people, I am going to recommend storing the image as a blob in the database. The convenience of using the database to join the image to the meta data will outweigh anyone's performance concerns at that scale. When you store the metadata in a database with a pointer to a file in the file system, it is too easy for things to get out of sync. The file gets moved, renamed, deleted etc; your database wont know and now your system is broken. Using a database will insure the integrity of your data, including the images for you.

Ted Cohen
  • 1,017
  • 10
  • 17
-1

First you will need to upload file in any predefined folder and than you can store name of that file in your database(with is varchar data type).

And when you will fetch those records, use that name to recreating image path wherever application required.

Abhijeet Dange
  • 133
  • 1
  • 11