How to assign unique names to images?

322 views Asked by At

I'm writing a web application where users can upload images. I wonder what's the best practice of assigning unique names to images ? I thought MD5 can ben useful and enough, but since each image has an owner, do I have to use MD5 ? I mean it could be enough to rename with Userid + imagename + creationdate ?

2

There are 2 answers

3
Eelvex On BEST ANSWER

Your database gives you a unique ID for each record. Though there are many different ways to do what you want, it is usually best to use this ID as a prefix/suffix to the name of the image. So, for these records:

ID  |  name
15    image.png
23    image.png

you could display something like: 15_image.png, 23_image.png

0
Aleksi Yrttiaho On

No, it would not be enough as the user might want to upload several images with the same name.

The easiest solution is to use a sequence number with the image name. You can be certain that there is no other image with the same name and there will never be one. With a hash function there is always an ever so slight chance of collision.