Search code examples
phplinuximagedirectory-structure

Better solution for storing million images in folders?


I am planing to store my pictures in folders according to their types and sizes:

images/{OBJECT}/{OBJECT_ID}/{OBJECT_ID}_{SIZE}.jpg

// FOLDERS LOOK LIKE THIS:
images/product/14/14_thumb.jpg
images/product/14/14_large.jpg
images/category/124/124_large.jpg
...

According to this approach, I can easily reach any object with a small effort:

function get_image($OBJECT, $OBJECT_ID, $SIZE){
    ...
}
// get image
get_image('product', 14, 'large');

I think it is good enough for me, but I want to be sure... Maybe I am missing something.

So what do you think?


Solution

  • It's not good enough.
    Explode ID to some count of parts, and each part will be the name of the directory level.
    For example, ID 445578 you can store in
    /44/55/78/445578.jpg
    or
    /4/4/5/445578.jpg
    Size can be a suffix, yes.

    You need to do it, because you can have more than 32k of IDs, but folder not always can keep more than 32k of files.