Nichole A. Miler
Nichole A. Miler

Reputation: 1381

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?

Upvotes: 0

Views: 94

Answers (3)

user1196549
user1196549

Reputation:

Append a serial ID to every file name, such as an integer that you increment every time.

Upvotes: 1

Manudog
Manudog

Reputation: 161

Add the time in the file name.

With:

time()

Upvotes: 1

Haider Ali
Haider Ali

Reputation: 964

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.

Upvotes: 1

Related Questions