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

I am trying to create a website on which I need to save several files uploaded by the user (for example, images of some profiles, some xml files, etc.).

So what is the best way to store these files?

I am currently creating a new directory on the server for each new user that logs in and saves files for each user in the corresponding directory, but someone told me that this is not the best way.

So how can I store these files? I create a shared directory and name the files according to the user ID or something related to the user, or continue to create a new directory for each user.

+6
source share
2 answers

A workable solution will depend on your requirements, for example. how many users do you expect. A solution with one directory per user can quickly hit some file system limitations (for ext3, the maximum number of subdirectories in one directory is fixed to 32000).

One file for the user must be in order for a long time (see How many files in the directory are too many? ).

Finally, if your user base is growing, you can use a fixed number of shards (for example, based on the user ID) to limit both the number of directories and the number of files in a directory (for an example, see how git stores its objects in free format ).

+4
source

You have several options.

  • You can store them as files on the file system, outside of the web root.
  • You can store them as binary data in standard RMDBS, such as MySQL.
  • You can use a NoSQL database, such as CouchDB or Redis, which specialize in document storage.

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

+1
source

Source: https://habr.com/ru/post/893729/


All Articles