I
I
IvanIF2020-08-23 18:48:18
Images
IvanIF, 2020-08-23 18:48:18

What is the best way to store images for a website?

Let's assume that there is a thematic portal where users can publish articles. Each article can be attached from 0 to 20 images. Where is the best place to store images? Variants with a DB are not considered.

I identified 2 main options:

1) Storing images on the same server as the site itself in a tree directory system.
2) Storing images on a separate server in one folder.

Here are the questions and comparison criteria:

1) Storage capacity . Image names consist of a unique set of numbers. And the tree structure consists of 100 directories and looks something like this:

/photos/1/1/115347123471324.jpg
/photos/1/2/121347123471324.jpg
...

/photos/2/1/211347123471324.jpg
/photos/2/2/221347123471324.jpg
... How

long will 100 such catalogs last if, for example, 30,000 images are added per year?
I don't know if this is important, but the server is on Linux. What is the general limit on the number of files in one directory?
How many can fit in one single server directory?

2) The speed of work . It is clear that the speed of work directly depends on the number of images. The question is, when will the brakes start? When will there be 1000, 5000 or 10000 images in one tree structure directory?
And what will happen to a separate server in this case, where all the images are in one directory?

3) How will the storage of images and the site on the same server affect the operation of the site itself? Will this greatly affect the speed of the site?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
entermix, 2020-08-24
@IvanIF

Use a hash based directory structure Like
this:

// Convert path to hash structure while preserving directory and extension.
  function hash($path, $random = FALSE)
  {
    $hash = ($random) ? md5(time() + rand()) : md5($path);
    
    $path = pathinfo($path);

    $segments = array();
    
    if ($path['dirname'] != '.')
    {
      $segments[] = $path['dirname'];
    }
    
    $segments[] = substr($hash, 0, 2);
    $segments[] = substr($hash, 2, 2);
    $segments[]	= $hash . ((isset($path['extension'])) ? '.' . $path['extension'] : '');
    
    return implode('/', $segments);
  }


$storage_path = hash(date('/Y/m/d/').basename($file).'/', true);

PS
https://medium.com/eonian-technologies/file-name-h...
PPS
What are the dangers of a large number of folders?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question