How to Upload Files with PHP 7

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

Welcome folks I am back with another blog post. In this post we will be implementing How to perform File Upload Operation with PHP 7 from scratch. File Upload is one of the very important tasks in website development. Every website which has some functionality has file upload. Today in this post we will be looking at File Upload operation in PHP Programming language This process is illustrated in the next steps in a detailed fashion.

Before moving on to develop the application make sure you turn on the php File Upload directive present in the php.ini file of your web server. If you are using Xammp or any web server there is a configuration file present and in that there is a directive file_uploads. Make sure it is set to On.

 

 

Create a brand new index.html file. This will include the interface of the application. It will include the file upload button from which the users can upload the files to the server. Just copy paste the code given below.

 

 

 

After that write the server side code in the file which is mentioned in the form attribute i.e. upload.php. This file will contain the logic whenever the file is selected by the user and upload button is pressed then what server side code should run on the server. This file will contain that. Just copy paste the code written below and also read the explanation for each section of code.

 

 

In this section of the PHP script we are processing that whether the submitted file is an image or not. There is some bullet points to be noticed in the script which are as follows: $imageFileType holds the file extension of the file (jpg, png etc) $target_file specifies the full path of the file to be uploaded $target_dir = “uploads/” – it specifies the directory where the file is going to be placed after it has been successfully uploaded. Notice , you need to create the uploads directory first before running the application.

 

CHECK IF FILE ALREADY EXISTS

 

 

LIMIT FILE SIZE

 

 

LIMIT FILE TYPE

 

COMPLETE FILE UPLOAD PHP SCRIPT

 

Leave a Reply