When creating websites, is it better to have a lot of images in one folder, or have them separated?

It's something that has always been in my mind but I never researched enough about it, and googling didn't give me many answers.

So I have been building mostly sites for real estate agencies since I started, and I always liked to organize the images by folders, lets say in webroot/photos/$propertyId/ but sometimes I've seen websites storing all the images in a folder, and differencing them mainly by the name.

I never had to build a huge website, so this has been in my mind but didn't concern me too much, but I would like to know, generally which of the 2 ways is a better approach. Is there really a human noticeable performance difference when performing different action like searching/deleting those files?

Keep in mind that sometimes there's not a images limit on the galleries, so having one column per image path/name in the property's row is not often an option for me, I would have to use another table to relate them.

Also I would like to mention that for example in my last project I had to manage tens of GB's of images, we had like 300000 properties and they had lets say an average of 10 pictures per property.


Most file system limits how many files you can have in a directory, which means you are forced to use subdirs anyway if the amount can grow indefinitely. Beware there is a limit on number of dirs as well (which is usually lower, say 32000 on ext3)

If you can calculate the exact path to a file then there is no performance problem reading that file.

Listing files in a huge directory can get really, really slow. I know this from experience.

I'd recommend use the id and then the modulu (%) operator to figure out where to put the file. Simple and scalable. For instance the path to image with id 12345 could be created like this:

12345 % 1000 = 345

Ends up in:

  • Thanks, didn't know about the limit! So I was doing it right without knowing it :D
    – aleation
    Jun 13 '13 at 11:57

The alternative is to store the files themselves in a database (as large byte blocks). However, this turns out to have an impact on performance, and some say it's not really what databases were built for.

The one advantage of databases I've heard is that it's more secure - someone rummaging on the webserver's filesystem could grab the images, but it would be more difficult for them if it's in a database. (And of course, you lose that security if you cache images anywhere, which would be my one recommendation to help performance)

Also, it may be worth noting another alternative is hosting images with professionals like Amazon. It costs some money, but users are connected to servers close to themselves, and all your server needs to do is maintain the correct URLs for each image.

  • I don't really like storing them in databases, doesn't feel natural to me, also, the image protection could be achieved through .htaccess or the own script. Also if I want to let people upload unlimited pictures I would have to use a table apart and relate them through foreign keys, which ... dunno, I don't like to do that unnecessarily, but Thanks for your answer anyway, your opinion is welcome :)
    – aleation
    Jun 13 '13 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.