Question: I'm creating an site which allows users to upload photos. It's a linux server. 

I was wondering what would be the best directory structure to to save these images? And is there a limit of number of files a folder can have? 

Would it be better to save every photo uploaded by every user to one single folder and have a reference of each in the database. Or create a folder for each user and have all of their photos in it?

Answer: 
There is probably no limit on files, but depending on the filesystem, you want to limit the number of files in a directory to between 1,000 and 10,000. If you have a filesystem that doesn't index directories, even having 1,000 files is going to make file operations slow.

If your filesystem does index directories, you could easily have 100,000 files in one, but you wouldn't want to. Doing an `ls -l` on a directory with even 1k files is going to be slow; you would probably give up before it even started outputting on a directory with 100k files.

If there aren't going to be too many users, having a single directory per user is a good idea. If there are going to be lots of users (over 1k), I would recommend storing the files in directories of up to 1k files each before creating a new directory.
The only limit to the number of files a directory can hold in linux in the number of free inodes. You can determine this figure by executing:

```
df -i

```

The figure reported as IFree is the upper bound on the number of files you can hold in a directory, but be aware that this figure is for the whole filesystem. So if you put the maximum number of files in one directory, you will not be able to create any more files anywhere else in the filesystem.

A directory for each user would be the way to go