Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP file rename

Tags:

php

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

like image 854
Ibrahim Azhar Armar Avatar asked Sep 03 '10 12:09

Ibrahim Azhar Armar


People also ask

How can we rename file in PHP?

PHP rename() Function rename("images","pictures"); rename("/test/file1. txt","/home/docs/my_file. txt");

How rename file after upload in PHP?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) . '.

What is the purpose of the rename () function when creating a file?

rename() function is used to change the name of the file or directory i.e. from old_name to new_name without changing the content present in the file.


1 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.

like image 183
janosrusiczki Avatar answered Sep 22 '22 14:09

janosrusiczki