Uploading Files in PHP using Forms

When you have to allow users to upload files using HTML forms and PHP, it is quite simple. All you have to use is $_FILES super global and it will upload the file. You can also set additional options like giving the uploaded file a file name and selecting the upload directory. You can also verify whether the uploaded file is of certain type like images so that people just don’t upload random files that may compromise your website.

For uploading the files, we will have to build an HTML form and use the POST method to process it. We also have to add some additional parameter to the form which is enctype=”multipart/form-data”. This part tells how the form should be encrypted when it is processed. It can only be used with the POST method.

Follow the examples below to build a simple html form that allows users to browse the file in their system. Then after a file is selected, the form needs to be submitted. The submission is then processed by PHP, some verification is done and the file is moved to a directory on the server.

HTML Form:


  1. The use of MAX_FILE_SIZE hidden form field is to specify the maximum upload file size.
  2. The size is measured in bytes.
  3. This hidden field should be placed before the file browsing field in HTML.

PHP Script fileupload.php


  1. See how the uploading directory and user file name are used along with move_uploaded_file function.
  2. $_FILES handles the uploaded file and it is further processed into the directory giving it a file name using move_uploaded_file.
  3. The above script does not check whether the same file name already exists and will overwrite it directly.
  4. $_FILES superglobal stores various other information in an array.

Here is what it stores in an example:

You may also like

Comments are closed here.


Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers