Should I Store My Images in the Database or Folders

Should I store my images in the database or folders?

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

Storing images in a database versus a filesystem

If the images are user data, rather than part of your application's code or theme, then storing the images in the database is a good idea, because…

  • Backups are easier to manage if all you have to back up is the database. On the other hand, if you store some application data in the database and some in the filesystem, then you'll have to coordinate the backup schedules of your database and your filesystem to ensure that the two are consistent.

    If you have a database administrator at your disposal, then great! Your backups should already be taken care of. If not, then database backups may be slightly tricky to set up, but once you do have a backup system, it can be better than filesystem backups. For example, many database systems have support for streaming replication.

  • If your application is load-balanced and served by a pool of multiple webservers, then you'll either have to replicate the data to all of the machines, or share them among your servers using a network filesystem.

Of course, having the images on a filesystem also has its advantages, namely in performance and simplicity, since most webservers are built to serve static files. A hybrid approach could give you the best of both worlds:

  • The images stored in the database would be the authoritative data.
  • Your application can have a feature to extract them as files in their local filesystem as a kind of cache. That cache can be rebuilt at any time, since it is not authoritative.
  • The webserver can then serve the files directly from the filesystem.

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.

To Do or Not to Do: Store Images in a Database

If you on occasion need to retrieve an image and it has to be available on several different web servers. But I think that's pretty much it.

  • If it doesn't have to be available on several servers, it's always better to put them in the file system.
  • If it has to be available on several servers and there's actually some kind of load in the system, you'll need some kind of distributed storage.

We're talking an edge case here, where you can avoid adding an additional level of complexity to your system by leveraging the database.

Other than that, don't do it.

Storing images in DB vs in Folder Structure

As usual, it depends. You need to consider the usage pattern of the images and what features your DBMS provides.

Storing images in the database:

PROS

  • If the images are to be associated with entities in your database (say, a user), the database can take care of maintaining that relationship. If, on the other hand, images aren't associated to anything in the database, you will probably not want to store them in the database.
  • If your database supports it, you will be able to process files within a transaction (I believe MS SQL 2008 supports this, I don't know if others do).
  • If you need to store multiple versions of each image (say, because they change over time), it will probably be easier to do in the database than on the file system.

CONS

  • You will be putting a lot of strain on the database.
  • Backing up your database may take a long time.

Storing images on disk:

PROS

  • Making backups is trivial
  • Inspecting images etc. just requires a file browser, no need for a database client

CONS

  • Keeping the database's view of the image collection and the actual content on the disk in sync may be non-trivial, depending on the operations you will be performing on the images.

Of course, all these concerns are particularly valid if you store large numbers of images.

PHP to store images in MySQL or not?

Always depends of context, but usually, I store a user image on the filesystem in a folder called /content/user/{user_id}.jpg and try to bother the database as little as possible.

PHP Should I store image paths in a database?

Using Folders for Organization

Advantages: They are logically clear to someone fiddling with the system on the back end- that's about it really.

Disadvantages: 'harder' to clean up when you delete a company, etc. and you have to make sure none of your directory names overlap, generally more work from the get go.

Using Images in One Folder

Advantages It's technically a bit easier to clean up and not all that much work.

Disadvantages You'll have to write at minimum a very basic collision detection algorithm and a very basic 'random name generator'.

Using the Database to Store Images

Caution: Many lives have been lost in this argument!

Advantages: Referential integrity, backing up/restoring is simpler, categorization

Disadvantages: Fraught with pitfalls, potentially slower, more advanced storage/retrieval techniques, potential performance issues and increase of network requests. Also, most cheap hosting providers' databases are way too terrible for this to be a good idea.

I highly recommend just using a hashed file name and storing it (the filename) in the database and then storing the images in a folder (or many folders) on disk. This should be much easier in the long run and perform better in general without getting too complicated.

store image in database or in a system file?

Always store images, music files etc in system files on disk, and then store the url:s to them in the database. That will make it

1) faster

2) easier to configure security settings

3) better in any ways I can imagine



Related Topics



Leave a reply



Submit