totallyNotLizards
totallyNotLizards

Reputation: 8569

File Uploads - best practise for directory structure?

Context:

I've just written a PHP file upload handler that saves any incoming file to my site's upload directory. The upload directory is publicly accessible through the web, and currently each file is just dumped straight into it after being renamed with a unique ID generated by the database. The hander will allow any file type aside from JS and PHP, not just images. Number of files likely to be in the thousands eventually but it will take a couple of years for that to happen.

My question:

Is it a good or bad idea to have all uploaded files in the same directory on the server, and if not please explain why, and what would be better approach?

I notice that wordpress stores them in directories named for the date for instance.

The only real downside I can see is that an ls on the directory might be resource intensive once there are a few thousand files in there. Or is there limits placed on number of files in one dir, or total size of one dir (running Linux)?

slightly preemptive note about security:

The upload handler is secured against anyone but logged in administrator level users uploading files, and it also won't save PHP or JS files (atleast as far as I can reliably test for them using the type attribute in the $_FILES array) as these may be executed by the server. Another safeguard I plan to implement is an .htaccess file that blocks requests to certain filetypes, just incase. Any notes on this aspect would also be welcome but it's not part of this question.

Upvotes: 2

Views: 2857

Answers (2)

ethrbunny
ethrbunny

Reputation: 10469

If it were me I'd break it up by day or some time value. You are talking about (potentially) thousands of files of unknown size. You will eventually have to structure the storage around this and might want to move parts to other volumes, archives, etc.

I would also make sure each file is chmod -x and given some neutral suffix.

Edit: I'd agree with @NADH about the indexing too. Many OS's get fussy when there are thousands of files in a folder.

Upvotes: 1

Nadh
Nadh

Reputation: 7243

There is no set standard when it comes to organizing files. However, it's not a good idea to put all the files in one directory. It'll eventually start affecting the performance of the system, especially when it comes to indexing.

You can do what WordPress does, organize by year, month, and date. You can maybe also organize by filetype (extension), so have a PDF directory, an ZIP directory and so on, with maybe year/month/day directories in them for further classification.

Upvotes: 2

Related Questions