We need a system in which, by the name of the database, the name of the table and the id of the desired row, we could get the image corresponding to this row.

I see 3 options:
1) store the images themselves in the database
2) create a field in the table and store the addresses of the desired images there
3) to store images in a folder with the address of the type files / database / table / id

Now I use the third option. And everything would be fine, but on the Internet many times stumbled upon a similar question and the answer was always the second option.
So, what are the pros / cons of the second and third options (the first, for obvious reasons, disappears) and why everyone advises the second one?

  • Second. Because if you need to display a picture on the site, for example, you need an absolute URL path that will be built depending on your controller, and only the image name will be substituted. Convenience - user190134
  • one
    Yes, the third is clearly faster, it is not necessary to refer to the database at all. Truth must be done so that there would be no more than 1000 files / folders in one folder. Otherwise, access begins to slow down wildly. It is better to make several folders like table/id1 , table/id2 , etc. and put the files inside by some simple algorithm like the remainder of dividing the ID by the number of such folders - Mike

2 answers 2

I do not know why they advise the second, the third is more logical and more flexible. Imagine that you wanted to change the repository. In the third variant, this is just a change of one line in the config (I hope you are not hard-coding the way?).

And the first special advantages simply do not. Is that the integrity of the storage of all data, but this is a dubious advantage.

  • to store files from different databases (i.e. applications) in one directory? - etki
  • @Etki, and even from the same database in one directory is not always good to store. There are many options for posting directories. But in principle, this scheme does not change. For example, storage => { base => /var/storage, component1 => /subdir1, ...} and further according to needs. - PinkTux

I vote for the second . Imagine that the addresses to the pictures are scattered all over the Internet and users refer to you using the links like / directory / 1 / image / 5

By itself, a simple select from the database works - there is an absolute path to the picture.

Keeping links in the database is easy to change - even though the script, even just an update. You can even change some links to external ones. You can merge folders, or vice versa, divide them into 10 pieces - the main thing is to simply do it with a script, at the same time updating the database.

It is possible to put a part of pictures on one screw, and to get a part of pictures outside by http. Imagine that you need to save 5 billion pictures, and a million on the web server? No folder structure will help - you will need to give back a picture received from another server at the address stored in the database.

When storing files such flexibility is not and never will. The file system structure will stay with you forever.