unknown os platform
Rename image file if it exists before uploading in PHP | Whats-online.info
whats-online-info

Rename image file if it exists before uploading in PHP


Rename
2016/12/06 / Science and Tutorials

Rename image file if it already exists and maintain its extension before uploading using PHP.







You can download this script example at the end of this tutorial

Image renaming is an important aspect of programming, particularly in web designing. It prevents files overwriting thus the chances of losing data are minimal.

For this tutorial, I will show how to rename a file by checking first if the filename already exists on the image hosted folder. If it already exists, rename the image file in PHP as shown below.




First, we need to create an HTML form to handle the file chooser and a submit button. 
We will set the form action to the current script, enctype multipart and our submission method to post as shown below.

<form action="<?php echo $_SERVER["SCRIPT_NAME"];?>" enctype="multipart/form-data" method="post"> <label>Select Image </label><br/><br/> <input type="file" name="upload" <br/> <br/><input type="submit" value="upload"/> </form>

For the PHP code, We will use a while loop in conjunction with file _exist function to check if the filename exists.
If the filename does not exist in the image hosted folder on the server, we will bypass the renaming and upload it to the server otherwise rename it before uploading as shown below.

Check if file exists in the hosted folder

while(file_exists($target_path)){ //set a new filename //add the filename to the image file directory }

This tutorial gives you the three best ways for which you can rename your image. We will create three PHP functions to handle each of the following renaming styles.

1.Appending the current timestamp to the original filename.

function rename_append_timestamp($source, $tempfile){ $target_path ='uploads-timestamp/'.$source; while(file_exists($target_path)){ $t=time(); $target_path = ('uploads-timestamp/'.$t.'-'.$source); } move_uploaded_file($tempfile, $target_path); }

2.Appending the image size to the original filename plus a random number.

function renaming_appending_width_height($source, $tempfile){      $target_path ='uploads-sizes/'.$source; while(file_exists($target_path)){ $imagedetails = getimagesize($tempfile); $width = $imagedetails[0]; $height = $imagedetails[1]; $rand = rand(0, 1000); $fileName = $width.'-'.$height.'-'.$rand.'-'.$source; $target_path = ('uploads-sizes/'.$fileName); } move_uploaded_file($tempfile, $target_path); }

3.Appending a unique random number to the original filename.

function rename_appending_unique_id($source, $tempfile){        $target_path ='uploads-unique-id/'.$source; while(file_exists($target_path)){ $fileName = uniqid().'-'.$source; $target_path = ('uploads-unique-id/'.$fileName); } move_uploaded_file($tempfile, $target_path); }

And finally call our functions to our program.
Note: each of our functions has two parameters:-

$source  -  This is the name of the image file.
 $tempfile  - This is the name of the image file in the temporary location.

 Each function executes one at a time, so we need to comment two of them and allow one to run as shown below.

if(isset($_FILES['upload']['name'])){ $sourcefile= $_FILES['upload']['name']; tempfile= $_FILES['upload']['tmp_name'];        rename_appending_unique_id($sourcefile, $tempfile); //renaming_appending_width_height($sourcefile, $tempfile); //rename_append_timestamp($sourcefile, $tempfile); }

The entire PHP code

<?php if(isset($_FILES['upload']['name'])){ $sourcefile= $_FILES['upload']['name']; $tempfile= $_FILES['upload']['tmp_name'];        rename_appending_unique_id($sourcefile, $tempfile); renaming_appending_width_height($sourcefile, $tempfile); //rename_append_timestamp($sourcefile, $tempfile); } //Function to Rename by appending a unique id function rename_appending_unique_id($source, $tempfile){         $target_path ='uploads-unique-id/'.$source; while(file_exists($target_path)){ $fileName = uniqid().'-'.$source; $target_path = ('uploads-unique-id/'.$fileName); } move_uploaded_file($tempfile, $target_path); } //Function to Rename by appending width and height function renaming_appending_width_height($source, $tempfile){           $target_path ='uploads-sizes/'.$source; while(file_exists($target_path)){ $imagedetails = getimagesize($tempfile); $width = $imagedetails[0]; $height = $imagedetails[1]; $rand = rand(0, 1000); $fileName = $width.'-'.$height.'-'.$rand.'-'.$source; $target_path = ('uploads-sizes/'.$fileName); } move_uploaded_file($tempfile, $target_path); } //Function to Rename by appending timestamp function rename_append_timestamp($source, $tempfile){ $target_path ='uploads-timestamp/'.$source; while(file_exists($target_path)){ $t=time(); $target_path = ('uploads-timestamp/'.$t.'-'.$source); } move_uploaded_file($tempfile, $target_path); } ?>

Note, this type of renaming only appends or adds an extension to the initial file name. The advantage of this kind of renaming is, it is SEO friendly. The original image file name is maintained.

I hope this tutorial has helped you. 

Follow us on Facebook, Twitter, Quora and Google+ for more solved cases or tutorials or discussion.

Download Code












You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment