CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

PHP - $_FILES

PHP - $_FILES

$_FILES is one of the β€˜superglobal’, or automatic global, variables in PHP. It is available in all scopes throughout a script. The variable $_FILES is an associative array containing items uploaded via HTTP POST method.

A file is uploaded when a HTML form contains an input element with a file type, its enctype attribute set to multipart/form-data, and the method attribute set to HTTP POST method.

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

HTML File Input Element for File Upload

The following HTML script contains a form withinputelement offiletype βˆ’

PHP CODE
<input type="file" name="file">

This β€œinput type” renders a button captioned as file. When clicked, a file dialogbox pops up. You can choose a file to be uploaded.

The PHP script on the server can access the file data in $_FILES variable.

Properties of $_FILES

The $_FILES array contains the following properties βˆ’

  • $_FILES[β€˜file’][β€˜name’]βˆ’ The original name of the file that the user has chosen to be uploaded.
  • $_FILES[β€˜file’][β€˜type’]βˆ’ The mime type of the file. An example would be β€œimage/gif”. This mime type is however not checked on the PHP side.
  • $_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’][β€˜full_path’]βˆ’ The full path as submitted by the browser. Available as of PHP 8.1.0.
  • $_FILES[β€˜file’][β€˜error’]βˆ’ The error code associated with this file upload.

PHP File Upload Error Codes

Theerror codesare enumerated as below βˆ’

Error CodesDescriptionUPLOAD_ERR_OK (Value=0)There is no error, the file uploaded with success.UPLOAD_ERR_INI_SIZE (Value=1)The uploaded file exceeds the upload_max_filesize directive in php.ini.UPLOAD_ERR_FORM_SIZE (Value=2)The uploaded file exceeds the MAX_FILE_SIZE.UPLOAD_ERR_PARTIAL (Value=3)The uploaded file was only partially uploaded.UPLOAD_ERR_NO_FILE (Value=4)No file was uploaded.UPLOAD_ERR_NO_TMP_DIR (Value=6)Missing a temporary folder.UPLOAD_ERR_CANT_WRITE (Value=7)Failed to write file to disk.UPLOAD_ERR_EXTENSION (Value=8)A PHP extension stopped the file upload.

Single File Upload Form

The 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. Save this file in the document root folder of your Apache server.

Preview

The above HTML renders a button named β€œChoose File” in the browser window. To open a file dialog box, click the β€œChoose File” button. As the name of selected file appears, click thesubmitbutton.

Server-Side Script to Handle Single File Upload

The server-side PHP script (upload.php) in the document root folder reads the variables $_FILES array as follows βˆ’

PHP CODE
<?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>";
?&gt;

It will produce the followingoutputβˆ’

PHP CODE
Filename: abc.txt
Type : text/plain
Size : 556762
Temp name: C:\xampp\tmp\ph&lt;p&gt;D833.tmp&lt;/p&gt;
Error : 0

Multiple File Upload Form

In PHP, you can upload multiple files using the HTML array feature βˆ’

Preview

Now, change the PHP script (hello.php) to βˆ’

PHP CODE
&lt;?php
   foreach ($_FILES["files"]["name"] as $key =&gt; $val) {       
      echo "File uploaded: $val <br>";
   }
?&gt;

The browser will show multiple β€œChoose File” buttons. After you upload the selected files by clicking the β€œSubmit” button, the browser will show the names of files in response to the URLhttp://localhost/hello.htmlas shown below βˆ’