Miron
Miron

Reputation: 2137

Local file storage

From one project to run into difficulties in the organization of static storage for very large numbers of files. For images, creating a table Image and have a record on which to calculate the path of storage. Like this:

Code:

 $ image_id = 1665765;
 $ paddedId = str_pad ($ image_id, 20, '0 ', STR_PAD_LEFT);
 $ path = '/'. implode (DIRECTORY_SEPARATOR, str_split ($ paddedId, 2));

At the output: ==> webroor.upload/00/00/00/00/00/00/01/66/57/65/1665765.jpg

$ image_id is a field in the table of entities that need pictures ($ user-> image_id). This approach allows on the basis of one $ image_id specify the full path to the image without having to call the base. It works when we obviously know the format of the file (in this case, always. Jpg). But sometimes it is necessary that the user downloaded the images of different formats. And this approach is not practical, because have to make the spirit and join the table with files (Images), to file by id and calculate its expansion path. Due to the fact that the table images can be huge (or even in a separate database) decreases system performance. Please share with us your recipes in storage. Maybe someone has an idea how to do better.

Sorry for my english.

Upvotes: 2

Views: 258

Answers (1)

ozahorulia
ozahorulia

Reputation: 10084

There is two ways.

First - you should optimize your architecture to store full pathes for all files in the database. But it's not too easy with already existing project a huge number of content. The second - you have convert all uploaded files to one specific extension, .jpg for example, and all you troubles will be gone. Many highload social networks use this solution.

Upvotes: 1

Related Questions