Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to store user uploaded files in a website?

Tags:

file

web

storage

I'm trying to create a website in which I need to store a few user uploaded files (like some profile images, some xml files etc).

So what is the best way to store those files?

Currently, I'm creating a new directory on the server for every new user registered and storing the files for each user in their respective directory but someone told me it's not the best way.

So, how do I store those files? Whether I create a common directory & name the files as per the user-id or something related to the user OR keep creating a new directory for every user??

like image 583
ptamzz Avatar asked Jul 27 '11 23:07

ptamzz


People also ask

How do websites store files?

Most major modern web sites are dynamic — they store data on the server using some kind of database (server-side storage), then run server-side code to retrieve needed data, insert it into static page templates, and serve the resulting HTML to the client to be displayed by the user's browser.

Where do you put files on a website?

Your website files need to be uploaded inside the public_html, which is the document root folder for your primary domain name. Additional sites you are hosting on your account, such as subdomains, have their own document root folder. Their respective website files need to be uploaded under the respective folder.


2 Answers

A workable solution will depend on your requirements, e.g. how many users are you expecting. A one-directory-per-user solution might hit some filesystem limitations soon (for ext3, the max number of subdirectories in one directory is fixed to 32000).

One-file-per-user should be ok for a long while (see: How many files in a directory is too many?).

Finally, if your userbase grows you could use a fixed number of shards (e.g. based on the userid) to limit both the number of directories and the number of files per directory (as an example for this, see how git stores its objects in loose format).

like image 95
miku Avatar answered Oct 10 '22 20:10

miku


Well you have a few options.

  1. You can store them as files on the file system, outside the web root.
  2. You can store them as binary data in a standard RMDBS like MySQL.
  3. You can use a "NoSQL" database like CouchDB or Redis which are specialized in storing documents.

The best way will depend on your application, timeframe, and your experience. Option 1 would probably be the easiest. Option 2 would probably be the most flexible. Option 3 would likely be the best performing if it meets the needs of your documents.

like image 31
Alan B. Dee Avatar answered Oct 10 '22 21:10

Alan B. Dee