PHP | $_FILES Array (HTTP File Upload variables)

How does the PHP file handle know some basic information like file-name, file-size, type of the file and few attributes about the file which have selected to be uploaded? Let’s have a look what is playing behind the scene.
$_FILES is a two dimensional associative global array of items which are being uploaded by via HTTP POST method and holds the attributes of files such as:

Attribute Description
[name] Name of file which is uploading
[size] Size of the file
[type] Type of the file (like .pdf, .zip, .jpeg…..etc)
[tmp_name] A temporary address where the file is located before processing the upload request
[error] Types of error occurred when the file is uploading

Now see How does the array look like??

  • $_FILES[input-field-name][name]
  • $_FILES[input-field-name][tmp_name]
  • $_FILES[input-field-name][size]
  • $_FILES[input-field-name][type]
  • $_FILES[input-field-name][error]

Lets go through examples, how this array works in first example .
Example 1 :

[sourcecode language=”html”]

<?php
echo "<pre>";
print_r($_FILES);
echo "</pre>";
?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload Image">
</form>

[/sourcecode]

In the above script, before uploading the file

PHPuploadfile

Once when we select the file and upload then the function print_r will display the information of the PHP superglobal associative array $_FILES.

FilesArrayinformation

Example 2 : Add the html code followed by PHP script different files. Let’s make an HTML form for uploading the file
index.html

[sourcecode language=”html”]
<!DOCTYPE html>
<html>
<head>
<title>GeeksForGeeks</title>
<style type="text/css">
div
{
background: #4CB974;
text-align: center;
font-size: 20px;
padding: 30px;
color: #fff;
font-family: sans-serif;
}
</style>
</head>
<body>
<form action="file-upload-manager.php" method="post"
enctype="multipart/form-data" style="border: 1px solid #1f1f1f;
padding: 20px; position: absolute; top: 50%; left: 50%;
transform: translate(-50%, -50%);">
<!–multipart/form-data ensures that form
data is going to be encoded as MIME data–>

<h2>Upload File</h2>
<input type="file" name="photo" id="fileSelect"><br><br>
<input type="submit" name="submit" value="Upload"><br><br>
<!– name of the input fields are going to be used in our php script–>
<div> This Video is made for GFG</div>
</form>
</body>
</html>
[/sourcecode]

Now, time to write a php script which is able to handle the file uploading system.
file-upload-manager.php

[sourcecode language=”php”]
<?php
// Check if the form was submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Check if file was uploaded without errors
if (isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0) {

$file_name = $_FILES["photo"]["name"];
$file_type = $_FILES["photo"]["type"];
$file_size = $_FILES["photo"]["size"];
$file_tmp_name = $_FILES["photo"]["tmp_name"];
$file_error = $_FILES["photo"]["error"];

echo "<div style=’text-align: center; background: #4CB974;
padding: 30px 0 10px 0; font-size: 20px; color: #fff’>
File Name: " . $file_name . "</div>";

echo "<div style=’text-align: center; background: #4CB974;
padding: 10px; font-size: 20px; color: #fff’>
File Type: " . $file_type . "</div>";

echo "<div style=’text-align: center; background: #4CB974;
padding: 10px; font-size: 20px; color: #fff’>
File Size: " . $file_size . "</div>";

echo "<div style=’text-align: center; background: #4CB974;
padding: 10px; font-size: 20px; color: #fff’>
File Error: " . $file_error . "</div>";

echo "<div style=’text-align: center; background: #4CB974;
padding: 10px; font-size: 20px; color: #fff’>
File Temporary Name: " . $file_tmp_name . "</div>";

}
}
?>
[/sourcecode]

In the above script, once we submit the form, later we can access the information via a PHP superglobal associative array $_FILES. Apart form using the $_FILES array, many in-built functions are playing a major role. After we are done with uploading a file, in the script we will check the request method of the server, if it is POST then it will proceed otherwise the system will throw an error. Later on, we have accessed the $_FILES array to get the file name, file size, and type of the file. Once we got those pieces of information print information of the file using echo.

Output:

Reference :http://php.net/manual/en/reserved.variables.files.php



My Personal Notes arrow_drop_up

Worked as a full stack developer in three projects as well as a freelancer with 47/5 rating and 100% completion rate

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.