Upload File Using PHP

Introduction

In this article we will learn the basic steps for uploading a file into the server or a destination path using PHP. The PHP file system supports file uploading with verification for the extension and size to make a secure upload and save the file into the destination path. Also the file gets a new unique file name after uploading. This article will help you to learn how to upload text, PDF, Docs image and many more files and validate the file while uploading it.

Basic Steps

  • Create HTML file
  • Create  PHP file
  • Final code with output 

Create HTML file

Creating a HTML file for uploading the file is not a big task. In the HTML file we use the file upload tag for fetching the file and also a button to submit it. The code of the HTML file is written below.

  1.  <HTML>  
  2. <HEAD>  
  3. <TITLE>File upload</TITLE>  
  4. </HEAD>  
  5. <BODY>  
  6.         <form action="upload.php" method="POST" enctype="multipart/form-data">  
  7.             <input type="file" name="file" />  
  8.             <input type="submit" value="upload" />  
  9.         </form>  
  10. </BODY>  
  11. </HTML>  
HTML
Create  PHP file

The PHP code for uploading the file is the main task. step-by-step we will learn. In the first step we get the file using $_FILES['file']. $_FILES, a "supergloabal" variable; this will store the information in an associative array. regarding the name of the file, size of the file, error and type, so let's see it.

Step 1

We will get all the file property values and we keep these values in a variable, for further use.  

  1. <?php  
  2. if(isset($_FILES['file'])){  
  3.    print_r( $file=$_FILES['file']);  
  4. }  
  5. ?>  
Step1
Step 2

The next step is for handling the file extensions. First of all we get the extension of the file and compare it with our saved file extensions, if the file extensions are not matched then the file will not be uploaded, in this code I type cast the file extension to reduce the ambiguity problems. 

 

  1.  $file_extexplode('.',$file_name);  
  2. $file_ext=strtolower(end($file_ext));  
  3. $allowarray('txt','jpg');  
Step 3

In this step we assign a unique file name with extensionusing the function uniqid(). This function generates the unique ID. This can be useful, for instance, if we are generating identifiers simultaneously on several hosts that might happen to generate the identifier at the same microsecond.

  1. $file_name_new= uniqid('',true) . '.' .$file_ext;  
  2. $file_dest ='D:\upload/'.$file_name_new;  
step 4   

This is the final stage where we move our file to the desired location using the move_uploaded_file() function. This function takes two argument values, the first is filename and the second is the destination from where the file will be stored. This function uses a HTTP POST upload. 

  1. if(move_uploaded_file($file_temp,$file_dest))  
  2. echo " file is saved successfully  at - "$file_dest;  
Final code with output 
  1. <?php  
  2.  if(isset($_FILES['file'])){  
  3.    $file=$_FILES['file'];  
  4. // file properties  
  5.    $file_name=$file['name'];  
  6.    $file_temp=$file['tmp_name'];  
  7.    $file_size=$file['size'];  
  8.    $file_error=$file['error'];  
  9.    
  10. // file extension  
  11.    $file_extexplode('.',$file_name);  
  12.    $file_ext=strtolower(end($file_ext));  
  13.    $allowarray('txt','jpg');  
  14.      if(in_array($file_ext,$allow)){  
  15.        if($file_error===0){  
  16.          if($file_size<=2097152){  
  17.               $file_name_new= uniqid('',true) . '.' .$file_ext;  
  18.               $file_dest ='D:\upload/'.$file_name_new;  
  19.                  if(move_uploaded_file($file_temp,$file_dest)){  
  20.                       echo " file is saved successfully  at - "$file_dest;  
  21.                      }  
  22.    
  23.                    }  
  24.                 }  
  25.          }  
  26.    }  
  27. ?>  

The following  is the Step-by-step output of the programme.

stepfinal1 

Select an image file for upload.

stepfinal2

Image selected and the clicking the upload button.

stepfinal3

After clicking, the file will be saved in the user-specified location. As we are seen in this browser output screen.

stepfinal4

Now we can see the file is also present in the user-specified location with the unique name.
 
Summary

In this article we learned the file upload mechanism in PHP, we also saw all the related functions that help to make upload a file. By this article we can also store our file and file related information in a database, also lots of other work. Thanks for reading this article, I hope this article will be helpful for you.

Up Next
    Ebook Download
    View all
    Learn
    View all