PHP file upload by using $_FILES array


PHP file upload by using $_FILES array

PHP file upload by using $_FILES array

Uploading files by using PHP

This chapter will take you through how to upload files by using PHP script. File upload is required in many scenarios to allow users of your site to select files from their local computers and upload to your website. These files may include image uploads, pdfs, audio or video files – depending on the requirements.

What is PHP $_FILES?

PHP provides $_FILES, which is a built-in array. The PHP $_FILES array contains all information regarding uploaded files.

Handling file attributes are managed in this array, for example, a file name, size, file type (.jpg, .gif, pdf, png etc.). This chapter will show you how to use the $_FILES array; see the following script.

An example of uploading a file by PHP

Let us go step by step to learn how to upload a file in PHP by using a real example:

Step 1 –  A basic user interface to select a file

First, we will go through uploading a single file by using the PHP script. The basic code below contains HTML code of a form.

It allows the users to select a file from the local computer and then press “Upload” button to upload a file on the hosting server (if you are practicing at the local computer, this is your server as well). Save this code as testupload.html

PHP file upload

Experience this example online



Step 2 – Uploading file to the server script

Now follow these points:

  • Create a PHP file and name it file_uploader.php. This is the file name that we used in our form action=”upload_file.php” in step 1. This file contains uploading script.
  • Create a folder and name it testupload. The location of the folder must be the same where upload_file.php exists (for this example).
  • Now place the following code in upload_file.php file.
Experience this example online



That’s it!

Now let us go through the upload_file.php code to learn what PHP script is doing!

  1. The $targetfolder variable contains the location where files should be uploaded. Change it as per the requirement of the project. In our example, you can see our target folder testupload is relative to our script file.  If your script file is in the following location, for example, www.yourdomain.com/site/upload_file.php then uploaded files will be at www.yourdomain.com/site/testupload.
  2. The file upload script then uses the move_uploaded_file() function to move uploaded file to the specified location, testupload, in that case. This function uses the $_FILE array.
  3. If the file is uploaded then it will show upload success message with the file name otherwise an error message will be generated.

Limiting the file types / Uploading images with PHP script

In order to limit users to upload only specified file formats in PHP upload file script, you can use the code below. For example you only want to allow images like .jpg, .gif, .png, pdf etc to be uploaded and not .exe or .php or other formats.

This is how you can do it:



As you can see in the script, the $file_type variable contains the file type that is handled in the if condition to limit file type uploads.

The next chapter explains how to increase the limit of file upload size.



Was this article helpful?

Related Articles

Leave A Comment?