Directory structure for large number of files

2019-08-03 23:21发布

问题:

I made one site... where i am storing user uploaded files in separate directories like
user_id = 1
so
img/upload_docs/1/1324026061_1.txt
img/upload_docs/1/1324026056_1.txt

Same way if
user_id = 2
so
img/upload_docs/2/1324026061_2.txt
img/upload_docs/2/1324026056_2.txt
...
n

So now if in future if I will get 100000 users then in my upload_docs folder I will have 100000 folders.
And there is no restriction on user upload so it can be 1000 files for 1 user or 10 files any number of files...

so is this proper way? Or if not then can anyone suggest me how to store this files in this kind of structure???

回答1:

img/upload_docs/1/0/10000/1324026056_2.txt
img/upload_docs/9/7/97555/1324026056_2.txt
img/upload_docs/2/3/23/1324026056_2.txt


回答2:

What I would do is name the images UUIDs and create subfolders based on the names of the files. You can do this pretty easily with chunk_split. For example, if you create a folder every 4 characters you would end up with a structure like this:

img/upload_docs/1/1324/0260/61_1.txt 
img/upload_docs/1/1324/0260/56_1.txt 

By storing the image name 1324026056_1.txt you could then very easily determine where it belongs or where to fetch it using chunk_split.

This is a similar method to how git stores objects.

As code, it could look something like this.

// pass filename ('123456789.txt' from db)
function get_path_from_filename($filename) {
    $path = 'img/upload_docs';
    list($filename, $ext) = explode('.', $filename); //remove extension
    $folders = chunk_split($filename, 4, '/'); // becomes 1234/5678/9
    // now we store it here
    $newpath = $path.'/'.$folders.'/'.$ext;
    return $newpath;
}

Now when you search for the file to deliver it to the user, use a function using these steps to recreate where the file is (based on the filename which is still stored as '123456789.txt' in the DB).

Now to deliver or store the file, use get_path_from_filename.