User uploads folder structure

2019-04-09 18:54发布

问题:

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!

回答1:

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:

  1. Create table in your database and put information about every single photo in it. Create photo filename from record id md5 hash.

    Database table example:

    +-----------------------------------------------------------------+
    | id  filename                            ext created_at  user_id |
    +-----------------------------------------------------------------+
    | 1   c4ca4238a0b923820dcc509a6f75849b    jpg 1381127206  1       |
    | 2   c81e728d9d4c2f636f067f89cc14862c    jpg 1381123406  2       |
    | ..                                      ..  ..          ..      |
    +-----------------------------------------------------------------+
    
  2. 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
    

Pros:

  1. You are not exposing directory structure to your users.
  2. It's easy to find file by it name.
  3. Information about files are stored in database so it's easy to sort, group ect.

Also ensure that directory indexing is enabled on the filesystem (it's enabled by default on newer systems).