用户上传文件夹结构 [英] User uploads folder structure
问题描述
随着网站的增长,用于用户上传的文件夹结构会对性能产生任何影响吗?
Can the folder structure for user uploads have any impact on performance as the site grows?
例如,我考虑了用于存储照片/相册的结构:
For instance, I considered this structure for storing photos/albums:
Public folder
└── Uploads
└── Users
└── User ID
└── Album ID - contains all photos in the album
相册中的所有照片!
Thanks in advance!
推荐答案
用户上传的文件夹结构会随着网站的发展而对性能产生影响吗?
Can the folder structure for user uploads have any impact on performance as the site grows?
是的。如果在单个目录中存储太多文件,则可能会减慢操作速度。发布的结构非常好。
Yes it can. If you store too many files in single directory it may slow down operations. Posted structure is very good.
编辑缺少一些解释,因此在下面我将其扩展:
Edit Maybe I was to fast with above answer and there's missing some explanations, so below I extend it:
如果您希望获得来自用户的成千上万张图片,则应考虑以下解决方案:
If you expect to have tens of thousands images from users, you should consider this solution:
-
在数据库中创建表,并将有关每张照片的信息放入其中。从记录ID md5哈希创建照片文件名。
Create table in your database and put information about every single photo in it. Create photo filename from record id md5 hash.
数据库表示例:
+-----------------------------------------------------------------+
| id filename ext created_at user_id |
+-----------------------------------------------------------------+
| 1 c4ca4238a0b923820dcc509a6f75849b jpg 1381127206 1 |
| 2 c81e728d9d4c2f636f067f89cc14862c jpg 1381123406 2 |
| .. .. .. .. |
+-----------------------------------------------------------------+
现在拆分md5哈希并从第一个字母(例如6个字母)创建目录结构:
Now split md5 hash and create directory structure from the first, let say, 6 letters:
uploads/
users/
photos/
c4/
ca/
42/
c4ca4238a0b923820dcc509a6f75849b.jpg
c8/
1e/
72/
c81e728d9d4c2f636f067f89cc14862c.jpg
优点:
- 您没有向用户公开目录结构。
- 通过名称轻松找到文件。
- 信息关于文件的信息存储在数据库中,因此很容易进行排序,分组等。
还要确保在文件系统上启用了目录索引(在较新的系统上默认启用)。
Also ensure that directory indexing is enabled on the filesystem (it's enabled by default on newer systems).
这篇关于用户上传文件夹结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!