I am creating a website that will require profile pictures to be uploaded for every user as well as resized versions. I will be using mysql to store IDs and others info for the images. I don’t ever want to have to deal with rearranging static files, so lets just assume this site gets thousands users. I am wondering what would be the best directory structure to store the images?
I have seen several mentioned ways previously:
1) md5(image_id), then if the hash was 49f68a5c8493ec2c0bf489821c21fc3b, the structure would be /49/f6/8a/5c/84/93/ec/2c/0b/f4/89/82/1c/21/fc/3b.jpg (or …..3b/filename.jpg). This way seems like it would be able to handle a lot, but looks like it might create a few TOO many directories. MAYBE a variation on this method?
2) /year/month/day/(possibly hour)/id.jpg
So what to do?
Drilling down subdirs on a unique hash like that is a good solution, but the number of subdirs in your example is way too many. Each two character subdir can support 256 entries, so if you’re going to have 5000 users, you’ll get only about 20 files per subdir when going just a single level deep, which is perfectly reasonable. Two levels deep will easily handle millions of users.
Also, I wouldn’t cut the filename to whatever remaining characters are on the hash. Use the full hash for the filename, regardless of how many levels deep you go. Files will be much easier to manage if you need to (for example) move them to a new store. I.e., don’t do this:
Do this: