Folder Structure for storing millions of images?

Kenny picture Kenny · Sep 4, 2012 · Viewed 7.9k times · Source

I am building a site that is looking at Millions of photos being uploaded easily (with 3 thumbnails each for each image uploaded) and I need to find the best method for storing all these images.

I've searched and found examples of images stored as hashes.... for example...

If I upload, coolparty.jpg, my script would convert it to an Md5 hash resulting in..

dcehwd8y4fcf42wduasdha.jpg

and that's stored in /dc/eh/wd/dcehwd8y4fcf42wduasdha.jpg but for the 3 thumbnails I don't know how to store them

QUESTIONS..

  1. Is this the correct way to store these images?

  2. How would I store thumbnails?

  3. In PHP what is example code for storing these images using the method above?

Answer

Mihai Iorga picture Mihai Iorga · Sep 4, 2012

How am I using the folder structure:

  • I'm uploading the photo, and move it like you said:

    $image = md5_file($_FILES['image']['tmp_name']);
    // you can add a random number to the file name just to make sure your images will be "unique"
    $image = md5(mt_rand().$image);
    $folder = $image[0]."/".$image[1]."/".$image[2]."/";
    
    // IMAGES_PATH is a constant stored in my global config
    define('IMAGES_PATH', '/path/to/my/images/');
    // coolparty = f3d40fc20a86e4bf8ab717a6166a02d4
    $folder = IMAGES_PATH.$folder.'f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // thumbnail, I just append the t_ before image name
    $folder = IMAGES_PATH.$folder.'t_f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // move_uploaded_file(), with thumbnail after process
    // also make sure you create the folders in mkdir() before you move them
    
  • I do believe is the base way, of course you can change the folder structure to a more deep one, like you said, with 2 characters if you will have millions of images.