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
There’s nothing really wrong with just using the database primary key to name the file.
The only real downside I see is that you end up with fairly useless filenames. Someone might complain down the road that more descriptive names would help with search engine rankings, etc.
As another answer points out, renaming a file to end in .png will not make it a png. You might want to take a look at PHP’s built-in GD library (or look up ImageMagick) if you want to actually convert image formats.