简体   繁体   中英

unique image name for each user

Imagine I'm building a blog system where a user can upload images for a blog post. I have a problem mapping the uploaded images with user and their blog post.

Here's the flow :

  1. User uploaded some pictures, it's saved to the server.

  2. I store the image name in the related table, means later I can retrieve images by blog's post_id.

But what if the user upload images that has the same file name?

You could go for a file name that's structured like

userID_blogpostID_filename

Oh and as for the same file name problem, just do a simple check, and rename the file if a file of the same name exists.

And as manudog's answer suggested, you could add the unix time along with the filename to avoid that from happening.

Add the time in the file name.

With:

time()

将序列号附加到每个文件名,例如您每次递增的整数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM