Should I store my images in the database or folders?

34,543

Solution 1

I've done it both ways recently and personally; I do prefer using the directory method for storing the images while keeping their properties in a DB.

Main reason: I had client to whom I made a website for. On the Webiste; there was a Photo Gallery section that allowed the user to upload new photos (which could be browsed from the public site). Since my client hasn´t thought on optimizing the images before uploading; the *.jpg was over 1mb. I did implement the ability to update the image (once it was saved to the DB) but it had to be done one record at a time.

If this happens while storing the images in a directory, then the files can be saved locally, optimized and put back onto the server.

Here is an example

Solution 2

I'd go for folders. More flexibility if you run out of sttorage space (just move them to another disk and re-point), more flexibility with other apps (eg. Silverlight). I'd only use DB for files that had to be secure.

Solution 3

For any normal site you absolutely want this as part of the site app itself, not stored in a DB. A web site should as much as possible be self contained to keep it portable, and not adding round trips to the DB (even where caching) can only be a good thing. Web servers are very good at serving image files.

However, I personally am working on an app where the images are dynamically created and made available to the site through a second management application. Clearly these must be DB backed in some form to keep the images maintainable and secure.

Long story short, where the images have business value (i.e. they're content, they need security, or they're dynamic) you're going to have to store them in a DB. Where they're static and trivial let the website be a website.

Solution 4

Two thoughts on this:

  1. Use the filesystem. But make sure you design your scheme well enough that any one folder is not overloaded with images and it becomes something of a nightmare to manage. For instance, you can tree it out with first or last letters of the identifier, or subsequent positional notation. Your mileage will vary, so make sure the scheme fits your data set (images) size. No need to go to detailed lengths if you're only managing, say, 20 images.

  2. Use SQL Server 2008. It has a new datatype called filestream while will save the images to the filesystem but allow you to retrieve it via standard database queries. See http://msdn.microsoft.com/en-us/library/cc949109.aspx for more information.

These aren't exclusive choices, but at the very least, I'd recommend option #1 for the sheer fact that you'll get better performance out of using a filesystem-based-scheme rather than storing and reading blobs out of a database.

Solution 5

I think saving images in system folders is the best way.
working with DB and Queries you know will cause some overloads and DB transanctions usually are heavy and it can place more stress on your server.
with the way u save images in folders and just put URLs in DB it is more suitable for your app loads.
but the advantage of DB is that you can BackUp your images.


My Suggestion: save your Images in folders. the only need is to know about system I/O operations.

Share:
34,543

Related videos on Youtube

Etienne
Author by

Etienne

Updated on July 09, 2022

Comments

  • Etienne
    Etienne almost 2 years

    Possible Duplicate:
    Storing Images in DB - Yea or Nay?

    Hi

    At the moment each Company on my website have 1 picture they can add to their profile. I save that image into the database....its their Company Logo.

    Now i want to allow them to add more pictures. Now i don’t know if i must save it all in the database or save it in folders????

    Reason why i think folders will be better is because there are so much nice articles with fancy silver light kinda features that i can use but all of them only cater for images saved in folders.

    And since i am not THAT good its hard for me to change the code to look at the database instead of the examples that uses folders for image retrieval.

    I would like to add something like this to my website (browsing through the images). Any code examples for me on how to do this when images are saved in the database? I am using ASP.NET with VB.net. Click here to view what i am talking about

    Any ideas guys?

    Regards Etienne

  • Dave Van den Eynde
    Dave Van den Eynde about 15 years
    If they're created dynamically, why store them at all?
  • annakata
    annakata about 15 years
    for reuse - it's a hell of a lot cheaper to use an extant image than create the same one every time
  • IrishChieftain
    IrishChieftain almost 15 years
    What do you mean by "secure" as regards storing them in the DB?
  • Program.X
    Program.X almost 15 years
    Assuming that your DB is secured effectively, it provides an extra layer. Attacker needs to identify images are stored in SQL, identify server, username/password, table. Simply scanning a file system on a compromised machine won't yield results and a badly written web site won't expose files through HTTP request techniques (eg leaving files downloadable). Works even better if the SQL Server is on a seperate machine/network.