Home » Php » PHP file rename

PHP file rename

Posted by: admin July 12, 2020 Leave a comment

Questions:

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)
Detail Picture

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.
title_1.jpg
title_2.jpg
title_3.jpg
title_4.jpg

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

How to&Answers:

Well, here’s a possible solution:

  1. 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 getimagesize($_FILES["ns_pic_title"]["tmp_name"]);
  2. Check your database for the maximum id of the image records and make the the $file_name variable 'title_'.($max_id + 1)
  3. At this point you should have $file_name and $file_extension so do move_uploaded_file($_FILES["ns_pic_title"]["tmp_name"], $ns_title_target.$file_name.'.'.$file_extension)

Hopefully this makes sense and helps.

Answer:

There are a couple of good options with various pros and cons.

Answer:

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 title, brief and detail directories already, it’s not really necessary to name each picture title_*, brief_*, and 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.

Answer:

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.