Open In App

How to get uploaded file information in Receiving Script in PHP ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will know how to get the uploaded file information in the receiving script in PHP.

When a document/file is uploaded to any application or any browser and to know the basic information of a file like a file name, type of file, and how many bytes are present in the file, PHP offers a predefined array called $_FILES that allows the receiving script to get the information of file/document. Let’s look into that process in detail.

When any document is uploaded from a web browser then it will be received by the webserver. The web server calls the PHP script specified in the form action attribute to process the file. The action attribute in the form tag defines the action to be performed when a form is submitted. Let’s visualize the process flow in the picture which is given below:

Process flow

To get the basic information of the uploaded files, PHP provides a $_FILES predefined array which is explained below in detail.

$_FILES Array: The $_FILES is a predefined & two dimensional associative global array that helps to receive a script to get the information about the uploaded file via the HTTP post method. It provides basic information about files like filename, size of the file, type of the file, etc. In receiving script the uploaded file information is organized in the $_FILES array as a two-dimensional array.

Let’s look how data of uploaded file is organized in $_FILES 2-dimensional array:

  • $_FILES[input-field-name][‘name’]: This shows the original file name on the system browser.
  • $_FILES[input-field-name][‘tmp_name’]: This shows the file with temporary file name, to be uploaded from browser was stored in the server.
  • $_FILES[input-field-name][‘type’]: This shows the file type which is determined by the web browser.
  • $_FILES[input-field-name][‘size’]: This specifies the number of bytes present in content of file.
  • $_FILES[input-field-name][‘error’]: This shows the error code associated with the uploaded file.

Approach:

  • Create a HTML Form that will accept normal text file.
  • Create a PHP file that will stores the uploaded file information using $_FILES array.
  • Specify the created PHP file at action attribute inside <form> tag.
  • Upload any file from the created web page.

We will understand the concept through the example, which accepts a file to upload and print the details of file uploaded.

Example: In this example, we have created a Form that accepts normal text file. This file will be upload followed by a PHP script which is able to handle the file uploading system.

HTML




<!DOCTYPE html>
<html>
<head>
<title>Getting the file information</title>
</head>
<body>
  <h1> Upload File Here</h1>
  <form action="getFileInfo.php" 
        method="post" 
        enctype="multipart/form-data">
      <input type="file" 
             name="simplefile" 
             id="fileSelect">
      <input type="submit" 
             name="submit" 
             value="Upload">
  </form>
</body>
</html>


Below is the getFileInfo.php script that handle the file uploading system.

Filename: getFileInfo.php

php




<?php
  if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Check if file was uploaded without errors
    if (isset($_FILES["simplefile"]) && $_FILES["simplefile"]["error"] == 0) {
       $file_name     = $_FILES["simplefile"]["name"];
       $file_tmp_name = $_FILES["simplefile"]["tmp_name"];
       $file_type     = $_FILES["simplefile"]["type"];
       $file_size     = $_FILES["simplefile"]["size"];
       $file_error    = $_FILES["simplefile"]["error"];
           
       echo "Uploaded File Name-" . $file_name . "<br>";
       echo "Temporary File Name-" . $file_tmp_name . "<br>"
       echo "Type of File-" . $file_type . "<br>";
       echo "Number of bytes-" . $file_size . "<br>";
       echo "Error-" . $file_error . "<br>";     
    }
 }
?>


Output:

 

Note: We can upload any text file or any normal file to perform this task.



Last Updated : 06 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads