在网站中存储用户上传的文件的最佳方法是什么? [英] What is the best way to store user uploaded files in a website?

查看:140
本文介绍了在网站中存储用户上传的文件的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个网站,我需要在其中存储一些用户上传的文件(例如一些个人资料图片,一些xml文件等).

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.

那么,如何存储这些文件?是否创建公共目录&根据用户ID或与用户相关的名称来命名文件,或者继续为每个用户创建一个新目录?

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??

推荐答案

一个可行的解决方案将取决于您的要求,例如您希望有多少用户.每个用户一个目录的解决方案可能很快会遇到一些文件系统限制(对于ext3,一个目录中子目录的最大数量固定为32000).

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?).

最后,如果您的用户群增加,则可以使用固定数量的分片(例如,基于用户ID)来限制目录数和每个目录的文件数(例如,请参见 git如何以宽松格式存储其对象).

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).

这篇关于在网站中存储用户上传的文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆