Home >>Advance PHP Tutorial >HomeHow to Rename image during upload

HomeHow to Rename image during upload

How to Rename image during upload

<?php

    if ($_POST['upload'] )

     {

     $user=$_POST['c'];

     //$filename=basename($_FILES["file"]["name"]);

     $tmp=$_FILES["file"]["tmp_name"];

      $extension = explode("/", $_FILES["file"]["type"]);

      $name=$user.".".$extension[1];

    move_uploaded_file($tmp, "upload/" . $user.".".$extension[1]);

echo "Your file is uploaded with ".$user." name";     

     }   

 ?>

<html>
   
<body>

     <form enctype="multipart/form-data" method="post">

    Your New file name<input type="text" name="c"/><br/>

    choose your file <input type="file" name="file"/><br/>

    <input type="submit" value="Upload" name="upload"/>

    </form>

   </body>

</html>

Output Your file is uploaded with phptpoint name your New file name choose your file
In the above example first enter your name whatever you want your new image name, second you have to get the extension of image using $_FILES['file']['type'] it returns the array. at the [0] index it shows image and at [1] index it shows your file extension like jpg, jpeg, png etc. now you have to put these new name like $user.".".$extension at the 2nd parameter of move_uploaded_file( ) function.

No Sidebar ads