How to Upload a File in PHP

File upload is quite easy and compact in PHP. We can upload single or multiple files in PHP with just a few lines of code.

PHP can handle uploaded files in POST requests with below constants.

File Upload Using PHP

Step 1: Create HTML Form To Upload File

In this step, we create a form to upload a file.

<!DOCTYPE html>
    <form action="upload_handle.php" method="post" enctype="multipart/form-data">  
      <label>Select File to Upload:</label>  
      <input type="file" name="file"/>  
      <input type="submit" name="submit"/>  

NOTE: enctype=”multipart/form-data” is required to upload files in PHP via POST request in forms.

Step 2: Create a PHP file to handle the Uploaded File

In this step, we create a new PHP file named upload_handle.php to handle the uploading process via PHP.

$target_path = "YOUR_PATH";  
$target_path = $target_path.basename( $_FILES['file']['name']);   
if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {  
    echo "File uploaded successfully...";  
} else{  
    echo "Error, file not uploaded...";  


  • We first define the $target_path variable where we want to store the uploaded file.
  • After that, we concatenate it with the original filename of the uploaded file which we found using basename() function.
  • Finally, we use the move_uploaded_file() function of PHP to move the uploaded file to our destination folder. 

Read Also: move_uploaded_file() function in PHP


In the above article, we learned the file upload process in PHP. We can upload any file via the above code to our server.

Enjoy Coding 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Read previous post:
Top 25 SQL Queries Interview Questions
Top 25 SQL Queries Interview Questions

If you are preparing for the database administrator (DBA) job you are at the perfect place. This post contains some mostly...