Yoi also need a unique id for each image if you allow images with the same name
Main Topics
Browse All TopicsI am buildung a function to allow my sites users to upload images to my server. My question is what's a good way to store them. I don't want to store them in the DB because u have limited MySQL space. So I can reference them in a folder, but is this also a good idea. I am expecting 1000+ images. Is it really a good idea to have that all in one folder.
What's your suggestions?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Yes, it's far better to store the actual files outside of the database and simply store the REFERENCE in the database.
As far as whether it's a good idea or not, that depends on how you're managing the images. I usually try not to have hundreds of files in any one folder. For example, I have one site that allows image uploads, and what I do is separate them into 2-level-deep folders using the first two letters of the filename, like this:
/images/p/h/photo1.jpg
/images/m/y/myphoto2.jpg
/images/p/h/photo2.jpg
/images/p/i/picture.gif
/images/s/s.gif <-- in case there's only one character in the fliename.
Alternatively, if you have users logging in and you have a user ID, you could separate them with a directory using the user ID:
/images/67/photo.jpg
/images/67/morephotosfromu
/images/83/userID83sphoto.
As to the "one folder" question, I think a thousand images in a folder is a lot but not too many. However if you had a situation with (for example) a hundred clients and ten images each, you could probably cut down the image lookup time by having a directory for each client . Thinking in random terms gives this picture:
1,000 images in a directory - average lookup scans 500 pointers.
100 directories - average lookup scans 50 directories
10 images - average lookup scans 5 pointers
A lot of this sort of thing is cached and you won't see as dramatic a difference as these numbers seem to indicate, but when you start to get a lot of images, it is worth considering a directory tree structure.
HTH, ~Ray
If you do have a chance of uploading multiple photos into the same directory and don't want to overwrite anything, you can just attach a timestamp to the filename, like:
photo-1234854022.jpg
If you're separating with user ID-named directories, then you should never have conflicting filenames (at least it would be EXTREMELY unlikely).
Also if you were to store the path of the image in a database using a unique identifier such as UserId-DateTime.ext then you could have a column for storing the user's filename for the image so that it's easier for them to recognise or remember.
In other words make it look as if the image is being saved with their chosen filename but in fact save it using your defined unique identfying method.
Then to prevent them trying to use duplicate names you could simply do a lookup against that pseudo filename (as it's not the actual filename) existing for the specific user (that also might have a tiny chance of users uploading the same image multiple times which would waste space on your server).
hth
Matt
Business Accounts
Answer for Membership
by: Ray_PaseurPosted on 2009-10-31 at 12:19:09ID: 25711124
Store them in a folder or folders on the file system. Store the file names in the data base. You might want to make up a unique name for the uploaded files from the user-id and a datetime string. That will help keep track of who did what and when.