I have a form where an admin will upload three pictures with different dimensions to three different designated directories. now to make sure that i don’t get into the problem of duplicate file names i implemented something like the php will compare the uploaded file name and it will check if that file name exist in the designated directory if yes then it will echo an error and stop the script execution.
Now one of my friend suggested me that it is very bad asking the admin to manually rename the picture file and asking them to take care of the file duplication problem. the solution he suggested was to rename the file automatically and then store it in the database and then direct it to directory.
I am confused about what combination should i give to the renamed file and also make sure it will remain unique file name to be more precise i would like you to understand my directory structure
as i said there will be three pictures files the admin will be uploading namely
a) Title Picture b) Brief Picture c)
and all the three picture files will be moved to the different respective directory, like title picture goes to title directory and so on.
i am using to script below currently just to move and store the file name with path using varchar in the database.
$ns_pic_title_loc= $_FILES["ns_pic_title"]["tmp_name"]; $ns_pic_title_name = $_FILES["ns_pic_title"]["name"]; move_uploaded_file($ns_pic_title_loc, $ns_title_target.$ns_pic_title_name) or die(mysql_error());
that is just the sample code i havent included the validation function which i am using. i was thinking like i want to rename all the files like
a) In title directory the file should be stored as.
and so on
and the same way to rest of the pictures. how do i do that? what function do i use to achieve my target. and if this is not the good way to rename the file i would appreciate any suggestion followed to rename the file.
thanks in advance
Well, here’s a possible solution:
- Get uploaded filename from
$_FILES["ns_pic_title"]["name"]and separate extension OR if we are only talking about image files get the image type with
- Check your database for the maximum id of the image records and make the the
'title_'.($max_id + 1)
- At this point you should have $file_name and $file_extension so do
Hopefully this makes sense and helps.
There are a couple of good options with various pros and cons.
Instead of having logic to figure out what the latest picture name is and calculate the next number increment, why not just use PHP’s tempnam() function? It generates an unique name with a prefix of your choice (i.e., “title”, “brief”, “detail”). You could also simply prepend a timestamp to the file name — if you don’t have a whole lot of admins uploading pictures at the same time, that should handle most name conflicts.
Since your pictures are going to be sorted into
detail directories already, it’s not really necessary to name each picture
detail_*, right? If it’s in the title directory, then it’s obviously a title picture.
Also, you’re going to be putting the file names in the database. Then elsewhere in the app, when you want to display a picture, I assume you are getting the correct file name from the database. So it isn’t really important what the actual file name is as long as the application knows where to find it. If that’s correct, it’s not necessary to have a very friendly name, thus a tempnam() file name or a timestamp plus the original file name would be acceptable.
Because you are storing references into the DB, I would prefer to just md5 the datetime and use that for the filename and store the disk filename to the DB also. It doesn’t matter what name it is written to disk with as long as you can point to it with the unique name into the DB.
I use this methodology, and in none of my testing does the disk name (md5 from the datetime) ever require multiple tries.