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

Ken*_*nny 13 php mysql storage photo

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

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

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

dcehwd8y4fcf42wduasdha.jpg
Run Code Online (Sandbox Code Playgroud)

并且存储在/dc/eh/wd/dcehwd8y4fcf42wduasdha.jpg 3个缩略图中我不知道如何存储它们

质询..

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

  2. 我如何存储缩略图?

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

Mih*_*rga 10

我如何使用文件夹结构:

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

    $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
    
    Run Code Online (Sandbox Code Playgroud)
  • 我相信是基础的方式,当然你也可以在文件夹结构改变为更深刻的一个,就像你说的,有2个字符,如果你将有百万计的图像.


sbe*_*rry 7

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

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

至于你的问题.

  1. 是的,这是存储它们的好方法.
  2. 我会这样做的方式

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

    或类似的

  3. 就如何实际存储图像而言,网上有很多例子.你有更具体的问题吗?