Upload a File in PHP:

You can upload a file on server with the help of HTML and PHP coding. The file firstly uploaded to the temporary folder and than it will relocate to the specified folder.

To upload a file on server you have to follow this process:

  • First of all create a HTML form and take a file element and a submit button when you take a input type file than on browser it will display a button written “browse” over it.
  • After that select a file which you want to upload
  • Than click on Submit button.
  • After this the selected file will be sent to the temporary folder on the server.
  • If you want to save the file on your location than assign the path.
  • Now the server sends back the result and confirms that the file is uploaded.


The file you want to upload it could be an image or a text file or a ppt file.

Upload Script in PHP:

To upload a file you have to use “$_FILES” global variable in PHP to fetch the file. This global variable holds all information about the file like the name of file, size of file etc.

Here is the list of information which can hold by $_FILES variable:

  • $_FILES[‘file’][‘tmp_name’]: this is the temporary directory on server where uploaded file save
  • $_FILES[‘file’][‘name’]: the original name of uploaded file
  • $_FILES[‘file’][‘size]: the size of uploaded file in bytes
  • $_FILES[‘file’][‘type]: the MIME type of uploaded file
  • $_FILES[‘file’][‘error]: the error code of uploaded file that is associated with file


 Example: The example is divided into two parts HTML and PHP


Now we will create a HTML form to upload a file and this HTML coding is same for all following examples in this topic


Now we will do the php coding and in following example we are printing all details of our uploaded file in an array pattern

In following PHP code we are displaying selected information of the file and the HTML code is as above only PHP code is changed

 Uploading File in Specified folder:

To upload a file in a specified folder first you have to create a folder and than use that folder name. To upload a file “move_uploaded_file()” function will be used. In this function you have to pass two parameters one is the temporary directory “$_FILES[‘file’][‘tmp_name’] “ and second is the file name “$_FILES[‘file’][‘name’]” .


In following example we are uploading a file in a specified folder the HTML code is same above


One response to “File Uploading in PHP”

  1. Andi says:

    Good day! This is my first comment here so I just wanted to give a quick shout out and say I really enjoy reading your posts.

Leave a Reply

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