Y
Y
Ytsu Ytsuevich2015-06-02 16:22:37
PHP
Ytsu Ytsuevich, 2015-06-02 16:22:37

How to name images in a database?

Let the avatars of users be stored along the path example.com/img/user/some_photo.jpg
What is better to store in the database?
Naturally, not an absolute path ...
I think /img should not be stored, you can add it along the way. But now the question is:
user/some_photo.jps or some_photo.jpg (and the User class will insert user/ )
Do I need to bother with this or just store it as /img/user/some_photo.jpg
Which one of you does what?
Are there any problems with one or another storage method?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
Y
Yuri Shikanov, 2015-06-02
@dizballanze

The less you store, the easier it will be to redo something.
The more you store - the less logic to generate the full url.
It seems to me that it is best to store either only the file name or the file name + the name of the generated directories (if the images are sharded into folders 01/some_img.jpg).

D
Dmitry, 2015-06-02
@thewind

user - changes? (Is this the user's nickname?)
If it changes, the script generates this path by the user's ID.
If it does not change, then why store this part in the database? Then you need to write down only the name of the file itself.
The /img/ folder is generally in the config

6
65536, 2015-06-02
@65536

when loading and after all resizes, I measure md5 and sha1 images, make up a path from them, it turns out like /images/0c/c1/c5/bd/3ed21a48.jpg in the database I store the compiled path and in a special field a conditional image identifier, for example users/ avatars/23, well, and another field for different versions of pictures and one more that combines these versions so that you can delete them in one motion. an additional advantage is that the same picture is physically stored once even if it is uploaded 100 times
gyazo.com/cbf5c71decc901855fb98b4d89e1c1e8

E
Evgeny Bukharev, 2015-06-02
@evgenybuckharev

When I load a user's avatar, I rename them like md5($userid.$salt), and store them in a folder, and usually generate thumbnails md5($userid.$salt.'100') or md5($userid.$salt .'origin'), with this approach, there is no need to store anything in the database, and it's not difficult to find the picture itself, we take the user's ID and hash it as we did it earlier with the picture, and we get the file name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question