views:

29

answers:

2

I'm wondering if there are any best practices for organizing files on the filesystem for a site that centers around users uploading files. (Not a hosting site like Imageshack, more like addons.mozilla.org)

Or am I over-analyzing this and should put everything in one folder?

A: 

First (and probably obviously), put the users' files in some dedicated place so they don't risk overwriting other stuff.

Second, if you expect lots of files then you may want to have subfolders. The easiest way to do that is to use the first letter of their filename as the folder.

So if I were to upload "smile.jpg", you could store it there: s/smile.jpg

If you're super popular and still have too many files, you can use more letters. And if you expect to have tons of users and you have tons of servers, you can imagine splitting the work by saving on s.example.com/upload/s/smile.jpg (but really if you have tons of servers then you probably already have a transparent way of sharing storage and load).

redtuna
+1  A: 

I tend to think about user uploads as just another kind of user data, and so it all goes into a database. Obviously, make sure the database you are going to use for this is a good choice for that, for example, a SQL database isn't necessarily right.

If it makes sense, I try to use a url pattern that makes sense in the context of the usage pattern of the site, for example:

example.com/username/users_file.jpg

If there's just no obvious way to do that, and I have to use a surrogate key, I just live with it:

example.com/files/abc123
example.com/files/abc123/
example.com/files/abc123/users_file.jpg

All three are the same file. in particular, the abc123 is all that the app needs to look up the file, the extra bit at the end is there so that browsers get a good hint at what the file should be named when it's saved to disk.

Doing it this way means that no matter what the original file is named, it always is unique to the user. Even if the user wishes to upload 100 files with the same name, all are unique.

TokenMacGuy