Folder Structure for storing millions of images? Folder Structure for storing millions of images? mysql mysql

Folder Structure for storing millions of images?


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 configdefine('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.


The reason you would use a method like that is simply to reduce the total number of files per directory (inodes).

Using the method you have described (3 levels deeps) you are very unlikely to reach even hundreds of images per directory since you will have a max number of directories of almost 17MM. 16**6.

As far as your questions.

  1. Yeah, that is a fine way to store them.
  2. The way I would do it would be

    /aa/bb/cc/aabbccdddddddddddddd_thumb.jpg
    /aa/bb/cc/aabbccdddddddddddddd_large.jpg
    /aa/bb/cc/aabbccdddddddddddddd_full.jpg

    or similar

  3. There are plenty of examples on the net as far as how to actually store images. Do you have a more specific question?


If you're talking millions of photos, I would suggest you farm these off to a third party such as Amazon Web Services, more specifically for this Amazon S3. There is no limit for the number of files and, assuming you don't need to actually list the files, there is no need to separate them into directories at all (and if you do need to list, you can use different delimeters and prefixes - http://docs.amazonwebservices.com/AmazonS3/latest/dev/ListingKeysHierarchy.html). And your hosting/rereival costs will probably be lower than doing yourself - and they get backed up.

To answer more specifically, yes, split by sub directories; using your structure, you can drop the first 5 characters of the filename as you alsready have it in the directory name.

And thumbs, as suggested by aquinas, just appent _thumb1 etc to the filename. Or store in separate folders themsevles.