How to Rename File Before Upload In Php

Create a upload name folder and index.php file
php rename uploaded file keep extension
Rename image while uploading in php


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

<input type="file" name="myfile" required/>
<br>new name <input type="text" name="newname" placeholder="name" required>
.ext <input type="text" name="ext" placeholder=".ext" required><br/>


<input type="submit" name="submit" />

</form>






<?php

if(isset($_POST['submit'])){



$name=$_FILES['myfile']['name']; // name of the upload file
$type=$_FILES['myfile']['type'];// type of the upload file
$size=$_FILES['myfile']['size'];// size of the upload file
$newname=$_POST['newname'];//renamefile name
$ext=$_POST['ext'];
$x=$newname.$ext;
$tmp_name=$_FILES['myfile']['tmp_name']; // tempoary name of upload file

$location="upload/".$x;// location where we upload files



//for check if file already exists or not
if(file_exists($name)){

echo "file already exists";


}else{

move_uploaded_file($tmp_name, $location);







}










}









?>








Previous
Next Post »
Thanks for your comment