In this tutorial we will learn how to files upload on remote server using a HTML form and PHP.

Creating an HTML form to upload the file

The following example will create a simple upload.php form that can be used to upload files.This form is having method attribute set to post and enctype attribute is set to multipart/form-data.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Upload file </title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container">
  <h2>Upload file</h2>
  <form action="upload_pro.php" method="post" enctype="multipart/form-data">
    <div class="form-group">
      <input type="file" class="form-control" id="img" name="img" accept="image/*">
    </div>
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>
</div>

</body>
</html>

Processing the uploaded file

Here's the complete code of our upload_pro.php file.

<?php
    if(!empty($_FILES))
    {	
        $img_name = time().$_FILES['img']['name'];
        $tmp_name = $_FILES['img']['tmp_name'];

        move_uploaded_file($tmp_name,"img/".$img_name);

        header("location:upload.php");
    }
    else
    {
        header("location:upload.php");
    }
?>

After you create img folder.

After execute this example by visiting the following URL in the browser and this URL may be different based on your site.

http://localhost/upload.php