How to rename file when upload file to server with PHP

Hi guys, In this tutorial I will show to you how you can rename a file when you upload it to server with PHP.

To upload a file, I will write first a simple HTML code:

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

<input type="file" name="file">

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

</form>

Then I will write a simple PHP code:

<?php
// $_FILES["file"] contains the information of my uploaded file
if(isset($_FILES["file"])):
$tmp_name = $_FILES["file"]["tmp_name"];
$new_name = "name_of_my_directory/".time().".pdf"; // .pdf is extension of my upload file and now I will upload it
move_uploaded_file($tmp_name,$new_name); // with this function I will upload files and I will rename them with $new_name
endif;
?>

Video Tutorial:









Leave a Comment