May 18, 2015

Storing images in database or in file system

Images in database:

Advantages:
  1. Secured
  2. Easy backup and Recovery
  3. Simple queries

Disadvantages:
  1. Large size of database
  2. Performance degradation of database with heavy images
  3. Slower access speed


Images in file system:

Advantages:
  1. Easier access
  2. Increased performance (database’s performance will not be affected)
  3. Faster access (File system access is faster than database access)

Disadvantages:
  1. Less secure
  2. Complex process to backup and restore
  3. Added complexity while storage
(Suggested way: Create an ID, copy the ID, change the filename of the image and store the image to the database)

Suggestion:

Store Images in file system if:
  1. Number of images are huge
  2. Database space is limited
  3. Available database is costlier
  4. No problem in securing the images (Try setting permission over the images)

Store Images in database if:
  1. Number of images are considerably less (Try storing images in separate table)
  2. Not worried about the size of the database space (If you have enough space)
  3. Database cost can be met
  4. Scared about the security over the images in filesystem



Tips:

  1. While the image is uploaded, check for the format of the image (Not all formats are viewable in the webpage)
  2. Use compression algorithms to compress the size of the image
  3. Check if the image uploaded is really an image (There are chances of uploading any PHP scripts to the image location)
  4. Create a thumbnail image for every image uploaded.
  5. While storing the image in the file system, create a table in the database with Auto-incrementing attribute that creates an ID for every image uploaded. Rename the image of any name to imageID.ext and imageID.thumb.ext (Naming conventions can be in any form that helps you to identify the image). In this way the images can be fetched from the file system instantly

No comments:

Post a Comment