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 ?
Asked
Active
Viewed 273 times
2 Answers
3
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

Eelvex
- 8,975
- 26
- 42
-
@Aleksi: you insert once, so it's not a big deal to ask it (if you want to save that way) and when you search, it's better to search by ID anyway. – Eelvex Feb 23 '11 at 02:30
-
1I agree that it's not a big deal, but when you consider the users with lowest skill level, it is a bit misguided to tell them that the ID justs pops up. You have to design for it and create a sequence or an auto-increment declaration depending of the DBMS is in use i.e. you'll have to ask for it as it's not given. – Aleksi Yrttiaho Feb 23 '11 at 02:37
-
@Aleksi: you're right, I haven't thought of it that way. I'll leave my answer as it is, though, until we see some more comments from the questioner. – Eelvex Feb 23 '11 at 02:43
0
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.

Aleksi Yrttiaho
- 8,266
- 29
- 36