What Is the PHP $_FILES, and How to Use $_FILES

Sometimes we want to upload a file in our HTML form. PHP provides the feature for handling a file at server side with $_FILES array. We can also check different properties of an files like file-name, file-type, file-size, and some other attributes.

Read Also: How to Upload File in PHP

What is $_FILES?

PHP provides one global variable called $_FILES. It is a two-dimensional associative array that keeps information related to uploaded files via the HTTP POST method.

It holds attribute as follows:

[name]Name of the file being uploaded
[size]Size of the file
[type]Type of file
[tmp_name]Temporary address where the file is located before processing the upload request
[error]Error code associated with the file

Above attributes syntax with $_FILES.

  1. $_FILES[file][name]
  2. $_FILES[file][tmp_name]
  3. $_FILES[file][type]
  4. $_FILES[file][size]
  5. $_FILES[file][error]

Lets, take a straightforward example that indicates different $_FILES attributes.

Example 1

   echo "<pre>";
   echo "</pre>";
<form action="" method="post" enctype="multipart/form-data">
   <input type="file" name="file">
   <input type="submit" value="Upload Image">

The above code displays the following HTML page to users.

File Upload in php

We select the file and upload it then print_r displays all information regarding the data. Suppose we upload download.png file then it will display the output as follows:


Array (
[file] => Array
[name] => errorsea.txt
[type] => Text Document (.txt)
[tmp_name] => 5EB63BBBE01EEED093CB22BB8F5ACDC3
[size] => 69696
[error] => 0

Steps being proceeded for uploading a file:

  1. The user first opens an HTML page containing form having text files, a browse button, and a submit button.
  2. The user clicks on the browse button and selects a file for uploading on the server.
  3. The full path for the selected file appears in the text field, and the user clicks on the submit button.
  4. The file is sent to a temporary location on the server.
  5. The PHP file assigned to a form in action attribute checks whether the file is arrived and then copies the file in the intended directory.
  6. If the file successfully uploaded, then PHP conforms success to the user.

Example 2

Suppose we want to create a form that takes an image file and upload it on the server.


  1. First, we create an index.html file that contains the code of HTML form.
  2. Then we create upload.php file that handles the uploading of the image.
Step 1: Code for index.html file
<!DOCTYPE html>
  <form action="upload.php" enctype="multipart/form-data" method="post">
    <h2>Upload file</h2>
    <input type="file" name="pic">
    <input type="submit" name="submit" value="Uplaod file"><br><br>
    // Here file name is used for accessing the file
Step 2: Code for upload.php file
  if(isset($_POST['submit'])) { 
    if(isset($_FILES["pic"]) && $_FILES["pic"]["error"] == 0) { 
      $name = $_FILES['pic']['name'];
      $size = $_FILES['pic']['size'];
      $type = $_FILES['pic']['type'];
      $temp_name = $_FILES['pic']['tmp_name'];
      $error = $_FILES['pic']['error'];

      echo "<div style='padding:15px;'> File Properties </div>";
      echo "<div style='padding:15px;'>Name : ".$name."</div>";
      echo "<div style='padding:15px;'>Size : ".$size."</div>";
      echo "<div style='padding:15px;'>Type : ".$type."</div>";
      echo "<div style='padding:15px;'>Temporary Name : ".$temp_name."</div>";
      echo "<div style='padding:15px;'>Errors : ".$error."</div>";

Once the file is uploaded successfully, then we can access all file attributes from $_FILES array. We can access file type, file size, and file name, etc.


php 7 File upload

Leave a Reply

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