大量文件的目录结构 [英] Directory structure for large number of files

查看:163
本文介绍了大量文件的目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一个网站...我将用户上传的文件存储在单独的目录中,如


user_id = 1
$ $ $ $ $ $


img / upload_docs / 1 / 1324026061_1.txt


img / upload_docs / 1 / 1324026056_1.txt




如果
$
user_id = 2


so


img / upload_docs / 2 / 1324026061_2.txt


img / upload_docs / 2 / 1324026056_2.txt


...


n

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

所以现在如果将来如果我将在我的upload_docs文件夹中获得10万个用户我将有100000个文件夹。


对用户上传没有限制,所以它可以是1000个文件的1个用户或10个文件的任何数量的文件...



这是正确的方式吗?
或者如果没有,任何人都可以建议我如何将这些文件存储在这种结构中?

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

推荐答案

我会做的是命名图像UUID,并根据文件的名称创建子文件夹。您可以通过 chunk_split 。例如,如果您每4个字符创建一个文件夹,最终将会出现如下结构:

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 

通过存储图像名称1324026056_1.txt,您可以很容易地确定它所在的位置,或者在哪里可以使用 chunk_split

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.

这是一个类似于git存储对象的方法。

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;
}

现在,当您搜索文件以将其传递给用户时,请使用功能使用这些步骤重新创建文件的位置(根据文件名仍然存储在DB中的123456789.txt)。

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

现在交付或存储该文件使用 get_path_from_filename

Now to deliver or store the file, use get_path_from_filename.

这篇关于大量文件的目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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