Reputation: 319
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?
Upvotes: 2
Views: 1042
Reputation: 53553
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:
49/f68a5c8493ec2c0bf489821c21fc3b.jpg
Do this:
49/49f68a5c8493ec2c0bf489821c21fc3b.jpg
Upvotes: 3
Reputation: 13435
I typically store the key in the DB next to the image record in a format like this:
userid/md5(image_id)_time.ext
This is good because it keeps people from being able to 'steal' your entire collection without doing a bunch of work. Plus, it helps name collision, in case you update the original picture and want to store the 'old' one for some amount of time (which can be beneficial in certain circumstances). Plus, you can set it to never-expire, since you'll never update it again. You may periodically need to go in and flush 'old' files, but that's a different concern.
Upvotes: 0
Reputation: 12925
images/first two chars of md5(user_id)/user_id/*.jpg
This way you haven't directories with thousand of other files/directories, and avoid having too much nested trees
e.g.
images/a9/1000/foo.jpg
images/a9/1000/bar.jpg
images/a9/107/baz.jpg
images/1f/24/goo.jpg
Upvotes: 0
Reputation: 416
If I am storing images with the filename as just an id, I tend to use the following structure;
/0/1.jpg
/500/501.jpg
/1000/1001.jpg
/1500/1501.jpg
The idea is to create folders of no more than 500 images, and numbering the base folder as the starting point. This doesn't require any special DB fields or hashing and you could choose to have more or less than 500.
Upvotes: 0