How to name uploaded files in php to prevent them from being overwritten?

Posted by user156814 on Stack Overflow See other posts from Stack Overflow or by user156814
Published on 2010-04-25T03:29:42Z Indexed on 2010/04/25 3:33 UTC
Read the original article Hit count: 208

Filed under:
|
|

I'm trying to add user submitted articles to my website, (only for admins). With each article comes an option to upload up to 3 images. My database is set up like this

Articles

id
user_id
title
body
date_added
last_edited

Photos

id (auto_increment)
article_id

First I save the article in the database, then I upload the photo (temporarily) then I create a new photo record in the database saving the article_id. Then I rename the uploaded photo to be the same as the primary key of the photo record, and to be a png.

$filename = $photo->id . '.png';

I figured this would be a good way to prevent files form being overwritten. This seems flawed to me. Any suggestions on how I should save my records and photos?

Thanks

© Stack Overflow or respective owner

Related posts about upload

Related posts about php