开发者

How to assign unique names to images?

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 + imagena开发者_运维技巧me + creationdate ?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜