Answer the question
In order to leave comments, you need to log in
Proper storage of images on the server
Can you please tell me how to implement a mechanism for storing images in the file system and links to these images in MySql? Preferably in detail. And is it correct to store images on the same server as MySQL? Thanks in advance.
Answer the question
In order to leave comments, you need to log in
You can store the file name (good.jpg) and the image type (goods), for example, a picture of a product, build a path based on this information, as you like, for example:
/media/origin/goods/g/go/good.jpg
/ media/origin/goods/t/to/goods.jpg
If you want to save a picture with the same name, then add an index to the end, for example:
/media/origin/goods/g/go/good.jpg
/media/origin/goods /g/go/good__1.jpg
/media/origin/goods/g/go/good__2.jpg The
subfolders after the image type /g/go/ are needed so that too many files do not fall into one directory.
If numbers are used as the file name (for example, these are indexes of records in the database), then it is better to form subfolders from the end of the file name, for example:
125.jpg -> /5/2/125.jpg
126.jpg -> /6/2/126.jpg
this will distribute files more evenly across folders.
This option will allow in the future to change the storage location of images, change the logic of forming the path to the image, etc.
Cached images will be stored accordingly, for example, along the following path /media/cached/goods/<preset name (200x120r)>/go/g/
good.jpg filter
Weird question. Do you think that there are some tricky ways to store images?) Put a picture on the disk, put the path to it in the database. All.
You have many servers for storing pictures. Maybe. the same ones that the scripts run on, it doesn't matter. You keep a special application for uploading pictures on these servers. When a picture arrives, randomly select one of the servers (keep the list in MySQL/Redis/whatever), upload the picture, get a link back. Write a link to the database img123.myproject.com/123/45/38475.jpg.
If the server dies, raise a new one, give it the name img123.myproject.com, restore from the backup, everything works again. Of course, you need to distribute pictures not through the application, but directly through nginx. Riak is
also good for storing pictures . The upside is that you get sharding, resharding, and replication out of the box, and you don't have to think too much about anything.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question