- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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