文件夹结构用于存储数百万张图像?

时间:2021-04-30 16:51:13

I am building a site that is looking at Millions of photos being uploaded easily (with 3 thumbnails each for each image uploaded) and I need to find the best method for storing all these images.

我正在建立一个网站,正在查看数百万张照片上传很容易(每张图片上传3张缩略图),我需要找到存储所有这些图像的最佳方法。

I've searched and found examples of images stored as hashes.... for example...

我搜索并找到了存储为哈希的图像示例......例如......

If I upload, coolparty.jpg, my script would convert it to an Md5 hash resulting in..

如果我上传,coolparty.jpg,我的脚本会将其转换为Md5哈希导致..

dcehwd8y4fcf42wduasdha.jpg

and that's stored in /dc/eh/wd/dcehwd8y4fcf42wduasdha.jpg but for the 3 thumbnails I don't know how to store them

这存储在/dc/eh/wd/dcehwd8y4fcf42wduasdha.jpg但是对于3个缩略图我不知道如何存储它们

QUESTIONS..

质询..

  1. Is this the correct way to store these images?

    这是存储这些图像的正确方法吗?

  2. How would I store thumbnails?

    我如何存储缩略图?

  3. In PHP what is example code for storing these images using the method above?

    在PHP中,使用上述方法存储这些图像的示例代码是什么?

5 个解决方案

#1


9  

How am I using the folder structure:

我如何使用文件夹结构:

  • I'm uploading the photo, and move it like you said:

    我正在上传照片,并像你说的那样移动照片:

    $image = md5_file($_FILES['image']['tmp_name']);
    // you can add a random number to the file name just to make sure your images will be "unique"
    $image = md5(mt_rand().$image);
    $folder = $image[0]."/".$image[1]."/".$image[2]."/";
    
    // IMAGES_PATH is a constant stored in my global config
    define('IMAGES_PATH', '/path/to/my/images/');
    // coolparty = f3d40fc20a86e4bf8ab717a6166a02d4
    $folder = IMAGES_PATH.$folder.'f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // thumbnail, I just append the t_ before image name
    $folder = IMAGES_PATH.$folder.'t_f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // move_uploaded_file(), with thumbnail after process
    // also make sure you create the folders in mkdir() before you move them
    
  • I do believe is the base way, of course you can change the folder structure to a more deep one, like you said, with 2 characters if you will have millions of images.

    我相信是基本的方式,当然你可以将文件夹结构更改为更深的文件夹,就像你说的那样,如果你有数百万张图片就会有2个字符。

#2


7  

The reason you would use a method like that is simply to reduce the total number of files per directory (inodes).

您使用这样的方法的原因只是减少每个目录(inode)的文件总数。

Using the method you have described (3 levels deeps) you are very unlikely to reach even hundreds of images per directory since you will have a max number of directories of almost 17MM. 16**6.

使用您描述的方法(3级深度),您每个目录甚至不可能达到数百个图像,因为您将拥有近17MM的最大目录数。 16 ** 6。

As far as your questions.

至于你的问题。

  1. Yeah, that is a fine way to store them.
  2. 是的,这是存储它们的好方法。
  3. The way I would do it would be

    我会这样做的方式

    /aa/bb/cc/aabbccdddddddddddddd_thumb.jpg
    /aa/bb/cc/aabbccdddddddddddddd_large.jpg
    /aa/bb/cc/aabbccdddddddddddddd_full.jpg

    /aa/bb/cc/aabbccdddddddddddddd_thumb.jpg /aa/bb/cc/aabbccdddddddddddddd_large.jpg /aa/bb/cc/aabbccdddddddddddddd_full.jpg

    or similar

    或类似的

  4. There are plenty of examples on the net as far as how to actually store images. Do you have a more specific question?
  5. 就如何实际存储图像而言,网上有很多例子。你有更具体的问题吗?

#3


2  

If you're talking millions of photos, I would suggest you farm these off to a third party such as Amazon Web Services, more specifically for this Amazon S3. There is no limit for the number of files and, assuming you don't need to actually list the files, there is no need to separate them into directories at all (and if you do need to list, you can use different delimeters and prefixes - http://docs.amazonwebservices.com/AmazonS3/latest/dev/ListingKeysHierarchy.html). And your hosting/rereival costs will probably be lower than doing yourself - and they get backed up.

如果您正在谈论数百万张照片,我建议您将这些照片发送给第三方,例如Amazon Web Services,更具体地说是针对此Amazon S3。文件数量没有限制,假设您不需要实际列出文件,则根本不需要将它们分成目录(如果您确实需要列出,则可以使用不同的分隔符和前缀 - http://docs.amazonwebservices.com/AmazonS3/latest/dev/ListingKeysHierarchy.html)。而你的托管/重建成本可能会低于自己 - 并且他们会得到备份。

To answer more specifically, yes, split by sub directories; using your structure, you can drop the first 5 characters of the filename as you alsready have it in the directory name.

更具体地回答,是的,按子目录拆分;使用您的结构,您可以删除文件名的前5个字符,因为您已经在目录名称中使用了它。

And thumbs, as suggested by aquinas, just appent _thumb1 etc to the filename. Or store in separate folders themsevles.

和aquinas建议的拇指一样,只是在文件名中添加了_thumb1等。或者存储在单独的文件夹中。

#4


-3  

1) That's something only you can answer. Generally, I prefer to store the images in the database so you can have ONE consistent backup, but YMMV.

1)这是你能回答的问题。通常,我更喜欢将图像存储在数据库中,因此您可以拥有一个一致的备份,但是YMMV。

2) How? How about /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb1.jpg, /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb2.jpg and /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb3.jpg

2)怎么样? /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb1.jpg,/dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb2.jpg和/dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb3.jpg怎么样

3) ??? Are you asking how to write a file to the file system or...?

3)???您是否在询问如何将文件写入文件系统或...?

#5


-3  

Improve Answer.

改善答案。

For millions of Images, as yes, it is correct that using database will slow down the process

对于数百万的图像,同样如此,使用数据库将减慢该过程是正确的

The best option will be either use "Server File System" to store images and use .htaccess to add security.

最好的选择是使用“服务器文件系统”来存储图像,并使用.htaccess来增加安全性。

or you can use web-services. many servers like provide Images Api for uploading, displaying. You can go on that option also. For example Amazon

或者你可以使用网络服务。很多服务器都提供Images Api来上传,显示。你也可以选择这个选项。例如亚马逊

#1


9  

How am I using the folder structure:

我如何使用文件夹结构:

  • I'm uploading the photo, and move it like you said:

    我正在上传照片,并像你说的那样移动照片:

    $image = md5_file($_FILES['image']['tmp_name']);
    // you can add a random number to the file name just to make sure your images will be "unique"
    $image = md5(mt_rand().$image);
    $folder = $image[0]."/".$image[1]."/".$image[2]."/";
    
    // IMAGES_PATH is a constant stored in my global config
    define('IMAGES_PATH', '/path/to/my/images/');
    // coolparty = f3d40fc20a86e4bf8ab717a6166a02d4
    $folder = IMAGES_PATH.$folder.'f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // thumbnail, I just append the t_ before image name
    $folder = IMAGES_PATH.$folder.'t_f3d40fc20a86e4bf8ab717a6166a02d4.jpg';
    // move_uploaded_file(), with thumbnail after process
    // also make sure you create the folders in mkdir() before you move them
    
  • I do believe is the base way, of course you can change the folder structure to a more deep one, like you said, with 2 characters if you will have millions of images.

    我相信是基本的方式,当然你可以将文件夹结构更改为更深的文件夹,就像你说的那样,如果你有数百万张图片就会有2个字符。

#2


7  

The reason you would use a method like that is simply to reduce the total number of files per directory (inodes).

您使用这样的方法的原因只是减少每个目录(inode)的文件总数。

Using the method you have described (3 levels deeps) you are very unlikely to reach even hundreds of images per directory since you will have a max number of directories of almost 17MM. 16**6.

使用您描述的方法(3级深度),您每个目录甚至不可能达到数百个图像,因为您将拥有近17MM的最大目录数。 16 ** 6。

As far as your questions.

至于你的问题。

  1. Yeah, that is a fine way to store them.
  2. 是的,这是存储它们的好方法。
  3. The way I would do it would be

    我会这样做的方式

    /aa/bb/cc/aabbccdddddddddddddd_thumb.jpg
    /aa/bb/cc/aabbccdddddddddddddd_large.jpg
    /aa/bb/cc/aabbccdddddddddddddd_full.jpg

    /aa/bb/cc/aabbccdddddddddddddd_thumb.jpg /aa/bb/cc/aabbccdddddddddddddd_large.jpg /aa/bb/cc/aabbccdddddddddddddd_full.jpg

    or similar

    或类似的

  4. There are plenty of examples on the net as far as how to actually store images. Do you have a more specific question?
  5. 就如何实际存储图像而言,网上有很多例子。你有更具体的问题吗?

#3


2  

If you're talking millions of photos, I would suggest you farm these off to a third party such as Amazon Web Services, more specifically for this Amazon S3. There is no limit for the number of files and, assuming you don't need to actually list the files, there is no need to separate them into directories at all (and if you do need to list, you can use different delimeters and prefixes - http://docs.amazonwebservices.com/AmazonS3/latest/dev/ListingKeysHierarchy.html). And your hosting/rereival costs will probably be lower than doing yourself - and they get backed up.

如果您正在谈论数百万张照片,我建议您将这些照片发送给第三方,例如Amazon Web Services,更具体地说是针对此Amazon S3。文件数量没有限制,假设您不需要实际列出文件,则根本不需要将它们分成目录(如果您确实需要列出,则可以使用不同的分隔符和前缀 - http://docs.amazonwebservices.com/AmazonS3/latest/dev/ListingKeysHierarchy.html)。而你的托管/重建成本可能会低于自己 - 并且他们会得到备份。

To answer more specifically, yes, split by sub directories; using your structure, you can drop the first 5 characters of the filename as you alsready have it in the directory name.

更具体地回答,是的,按子目录拆分;使用您的结构,您可以删除文件名的前5个字符,因为您已经在目录名称中使用了它。

And thumbs, as suggested by aquinas, just appent _thumb1 etc to the filename. Or store in separate folders themsevles.

和aquinas建议的拇指一样,只是在文件名中添加了_thumb1等。或者存储在单独的文件夹中。

#4


-3  

1) That's something only you can answer. Generally, I prefer to store the images in the database so you can have ONE consistent backup, but YMMV.

1)这是你能回答的问题。通常,我更喜欢将图像存储在数据库中,因此您可以拥有一个一致的备份,但是YMMV。

2) How? How about /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb1.jpg, /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb2.jpg and /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb3.jpg

2)怎么样? /dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb1.jpg,/dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb2.jpg和/dc/eh/wd/dcehwd8y4fcf42wduasdha_thumb3.jpg怎么样

3) ??? Are you asking how to write a file to the file system or...?

3)???您是否在询问如何将文件写入文件系统或...?

#5


-3  

Improve Answer.

改善答案。

For millions of Images, as yes, it is correct that using database will slow down the process

对于数百万的图像,同样如此,使用数据库将减慢该过程是正确的

The best option will be either use "Server File System" to store images and use .htaccess to add security.

最好的选择是使用“服务器文件系统”来存储图像,并使用.htaccess来增加安全性。

or you can use web-services. many servers like provide Images Api for uploading, displaying. You can go on that option also. For example Amazon

或者你可以使用网络服务。很多服务器都提供Images Api来上传,显示。你也可以选择这个选项。例如亚马逊