PHP $_FILES


Introduction

The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.

$HTTP_POST_FILES also contains the same information, but is not a superglobal, and now been deprecated

The _FILES array contains following properties −

$_FILES['file']['name'] - The original name of the file to be uploaded.

$_FILES['file']['type'] - The mime type of the file.

$_FILES['file']['size'] - The size, in bytes, of the uploaded file.

$_FILES['file']['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.

$_FILES['file']['error'] - The error code associated with this file upload.

Following test.html contains a HTML form whose enctype is set to multiform/form-data. It also has an input file element which presents a button on the form for the user to select file to be uploaded.

<form action="testscript.php" method="POST" enctype="multipart/form-data">
   <input type="file" name="file">
   <input type ="submit" value="submit">
</form>

The PHP script is as follows:

Example

<?php
echo "Filename: " . $_FILES['file']['name']."<br>";
echo "Type : " . $_FILES['file']['type'] ."<br>";
echo "Size : " . $_FILES['file']['size'] ."<br>";
echo "Temp name: " . $_FILES['file']['tmp_name'] ."<br>";
echo "Error : " . $_FILES['file']['error'] . "<br>";
?>

Output

This will produce following result −

Filename: hello.html
Type : text/html
Size : 56
Temp name: C:\xampp\tmp\php32CE.tmp
Error : 0

Updated on: 21-Sep-2020

20K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements