Steven
Steven

Reputation: 19425

What is best practice when it comes to storing images for a gallery?

My question is not about storing images on disk or in DB.

My questions are:
- Should images be stored in one folder, or many folders?
- Is it ok to use md5 for creating unique id's? E.g. md5(id+filename+random_num)
- Should images be cached on server or on clients browser / computer?

Anything else I should think of?

The solution is using php, apache and mysql. We use Uploadify for uploading images.

Some code I use today

  /**
   * Calculate dir tree for object
   * Folders starts from 00 to FF (HEX) and can have just as
   * many subfolders (I think :)
   * @param $id - User ID
   * @param $type - Image category
   * @return string
   */
  function calculateDirTree($id, $type)
  {
      $hashUserID   = substr(hash('md5', $id), -4);
      $parentFolder = substr($hashUserID,0,2);
      $subfolder    = substr($hashUserID,2);    
      $basePath     = $type."/".$parentFolder.'/'.$subfolder.'/';

      return $basePath;
  }  

Upvotes: 11

Views: 5745

Answers (5)

PiTheNumber
PiTheNumber

Reputation: 23542

Should images be stored in one folder, or many folders?

You are talking about "100k - 200k images" so many folders is a must have. Try to have max. ~1000 images in on folder.

Is it ok to use md5 for creating unique id's? E.g. md5(id+filename+random_num)

Yes, you can do this. It will avoid problems with long filenames.

Should images be cached on server or on clients browser / computer?

The should be cached on the client side. The problem with so many images is that it creates high traffic. Caching on the client help reducing this.

Upvotes: 5

Giorgio Minardi
Giorgio Minardi

Reputation: 2775

definetely go for the File System: it's more performant and fits better for storing files (that's what is made for). Sql can slow down when saving/retrieving large images. You can create a folder for each user (using the ID as the folder name) and when an image is saved on the file system you can save the reference on a UserImages table (by saving the filename against the user on sql). You can ensure that each image got a unique filename by renaming it when you saving it, you can use a combination of the original filename with the actual DateTime (no need of using MD5). Also, images should always be cached in order to save yours and the clients bandwith.

Upvotes: 0

user215361
user215361

Reputation:

Regarding the caching, it would be best to cache it at both ends, that way new images are retrieved quickly, and users visiting existing images have it cached.

I don't know of any filesystem limits regarding storing them in one or multiple folders.

Upvotes: 0

nfechner
nfechner

Reputation: 17525

Depending on the number of images you want to handle, I would strongly suggest using several folders. The easiest way should be to use the first letter of the file name to create a folder structure. I think, the numbers are something like this:

less than 1000 images  --> one folder
less than 20000 images --> one level of folders (a, b, c, ...)
more                   --> several levels (a containing aa, ab, b containing ba, bb, ...)

YMMV

Upvotes: 3

aacanakin
aacanakin

Reputation: 2913

I think using multiple folders or same folder is dependent on your web application. For instance, if there are multiple profiles with each profile having multiple images, you can use multiple folders with using folder names as profile names.

My last advice is if you have tons of images sha256 encryption algorithm is better for preventing collision

Upvotes: 0

Related Questions