Upload File in PHP

PHP file upload function allows you to upload any type of file. You can easily upload file in PHP to the server through a few lines of code. However, you have to be careful when developing upload script, because hackers will upload Web shell through your nonsecure upload script and will hack your website.

You can upload files through HTML forms and It will submit to PHP scripts for upload into the server. When you upload files it will be uploaded into a temporary directory and then moved to a target destination by a PHP script.

In this article, I will explain how to upload file in PHP.  Let’s start

Live Demo

PHP Configuration for Upload file in PHP

Before start developing the php script, you will need to make sure that your server PHP configuration allows you to upload file. You will need to find a php.ini file in your server to check PHP configuration.

If you don’t know where you can find a php.ini file, you can use the following function to find it. Just create a file on your server with .php extension and enter the following code. Now open it to your browser through the link.

<?php echo php_ini_loaded_file(); ?>

If you can’t edit the php.ini file, create new php.ini files in your website root directory. Enter the following text in php.ini file:

file_uploads = On
upload_max_filesize = 16M
max_file_uploads = 20 
post_max_size = 20M
max_input_time = 60
memory_limit = 128M
max_execution_time = 30

php.ini explained

By default, PHP is set to allow uploads of files with a size of 2MB or less. You can change the file upload size limit by change upload_max_filesize value in the php.ini file.

upload_max_filesize = 10M

If you changed the upload_max_filesize value, You must extend the post_max_size value. So that it is bigger than upload_max_size.

post_max_size = 20M

When you are uploading a file through PHP it takes some time to upload file, it’s called max_execution_time. If file upload time exceeds the max_execution_time limit, PHP function will stop uploading the PHP. So You can modify it to upload the bigger file.

max_execution_time = 60

When it exceeds this memory the server stops executing the script, It will show an empty page or no response from the server or you get a fatal error.

memory_limit = 80M

An Example: We extend file upload size up to 50 MB. So You can increase or decrease it whatever your requirements.

file_uploads = On
upload_max_filesize = 50M
post_max_size = 100M
max_execution_time = 300
memory_limit = 500M

Increasing upload file size in PHP using .htaccess

php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 128M

Alternatively, You can use .htaccess files to increase PHP upload file size instead of php.ini. Create a .htaccess file on your website root directory and enter the above code into your .htaccess file. That’s it.

 

Upload File in PHP

Now Let’s start developing a script to upload file in PHP: 

At first, We have to create an HTML form to submit and upload file in PHP:

<form action="upload.php" method="POST" enctype="multipart/form-data">

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

</form>

HTML file upload form explained

  • HTML form submitted to upload.php through action=”upload.php”
  • The HTML form must be submitted using method=”POST”
  • To upload the file you need to use the following attribute in HTML form: 
    enctype="multipart/form-data"
  • To upload file you have use input type=”file” instead of text in <input> tag.

 

After selecting a file into HTML form it will submit to upload.php

upload.php

Now create a file called upload.php. Then create a directory called upload. Now enter the following code in upload.php:

<?php
   if(isset($_FILES['data'])){
      $file_name = $_FILES['data']['name'];
      $file_size = $_FILES['data']['size'];
      $file_tmp = $_FILES['data']['tmp_name'];
      $file_type = $_FILES['data']['type'];
      $file_ext=strtolower(end(explode('.',$file_name)));
      $extensions= array("jpeg","jpg","png","txt");
      $target_dir="upload/";

      if(in_array($file_ext,$extensions)){

         if($file_size < 2097152) {

         move_uploaded_file($file_tmp,$target_dir.$file_name);
         echo "File Upload Successfully";
         }
         else{
            echo "Upload Failed! This file size is larger than 2 MB";
         }     
      }
      else{
         echo "Upload Failed! This file type is not allowed";
      }
   }
?>

Upload.php code explained:

  • $file_name specifies the uploaded file name.
  • $file_size specifies the uploaded file size.
  • $file_tmp specifies a temporary stored uploaded file name.
  • $file_type specifies the upload file type.
  • $file_ext is a separate file extension from the file name.
  • $extensions are the array of the allowed file extension.
  • $target_dir = “upload/” – specifies the directory where the file is going to stored.
  • The in_array() function searches an array for a specific value.
  • The move_uploaded_file() function moves an uploaded file to a new location (Upload Folder).

If you want to develop download file in PHP, Please read our following articles:

How to Download File in PHP

Conclusion

You can easily upload a file in PHP through this code. This script will filter your file extension, extend the file upload limit through PHP. So you easily configure it. But you to configured php.ini to upload files. I hope you understand how to upload files in PHP. You can be developed your own file upload script through this process.

If you have any questions about the file upload in PHP, Please comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here