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.
- <HTML>
- <HEAD>
- <TITLE>File upload</TITLE>
- </HEAD>
- <BODY>
- <form action="upload.php" method="POST" enctype="multipart/form-data">
- <input type="file" name="file" />
- <input type="submit" value="upload" />
- </form>
- </BODY>
- </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.
- <?php
- if(isset($_FILES['file'])){
- print_r( $file=$_FILES['file']);
- }
- ?>
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.
- $file_ext= explode('.',$file_name);
- $file_ext=strtolower(end($file_ext));
- $allow= array('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.
- $file_name_new= uniqid('',true) . '.' .$file_ext;
- $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.
- if(move_uploaded_file($file_temp,$file_dest))
- echo " file is saved successfully at - ". $file_dest;
Final code with output
- <?php
- if(isset($_FILES['file'])){
- $file=$_FILES['file'];
-
- $file_name=$file['name'];
- $file_temp=$file['tmp_name'];
- $file_size=$file['size'];
- $file_error=$file['error'];
-
-
- $file_ext= explode('.',$file_name);
- $file_ext=strtolower(end($file_ext));
- $allow= array('txt','jpg');
- if(in_array($file_ext,$allow)){
- if($file_error===0){
- if($file_size<=2097152){
- $file_name_new= uniqid('',true) . '.' .$file_ext;
- $file_dest ='D:\upload/'.$file_name_new;
- if(move_uploaded_file($file_temp,$file_dest)){
- echo " file is saved successfully at - ". $file_dest;
- }
-
- }
- }
- }
- }
- ?>
The following is the Step-by-step output of the programme.
Select an image file for upload.
Image selected and the clicking the upload button.
After clicking, the file will be saved in the user-specified location. As we are seen in this browser output screen.
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.