PHP $_FILES Array (HTTP File Upload variables)
Last Updated :
09 Aug, 2024
How does the PHP file handle know some basic information like file-name, file-size, type of the file and a few attributes about the file that has been selected to be uploaded? Let's have a look at what is playing behind the scene. $_FILES is a two-dimensional associative global array of items that are being uploaded via the 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]
Approach: Make sure you have XAMPP or WAMP installed on your machine. In this article, we will be using the XAAMP server.
Let us go through examples, of how this PHP array works in the first example.
Example 1 :
HTML
";
print_r($_FILES);
echo "
In the above script, before uploading the file
Once when we select the file and upload then the function print_r will display the information of the PHP superglobal associative array $_FILES.
output arrayExample 2: Add the HTML code followed by PHP script in different files. Let’s make an HTML form for uploading the file index.html
HTML
<!DOCTYPE html>
<html>
<head>
<title>PHP $_FILES Array</title>
<style type="text/css">
div {
background: #4CB974;
text-align: center;
font-size: 20px;
padding: 30px;
color: #fff;
font-family: sans-serif;
}
form {
border: 1px solid #1f1f1f;
padding: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<form action="file-upload-manager.php"
method="post"
enctype="multipart/form-data">
<!--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>
Now, time to write a PHP script which is able to handle the file uploading system. file-upload-manager.php
PHP
File Name: " . $file_name . "";
echo "
File Type: " . $file_type . "";
echo "
File Size: " . $file_size . "";
echo "
File Error: " . $file_error . "";
echo "
File Temporary Name: " . $file_tmp_name . "";
}
}
?>
Once we submit the form in the above script, we can later access the information via a PHP superglobal associative array $_FILES. Apart from 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 accessed the $_FILES array to get the file name, file size, and type of the file. Once we got those pieces of information print the information of the file using echo.
Output:
Reference: http://php.net/manual/en/reserved.variables.files.php
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
Write a Code To Upload A File in PHP? PHP makes it easy to upload files in web applications, whether it's for images, documents, or other types of files. With its built-in functions, uploading files to the server becomes a simple task. However, it's important to be cautious when allowing file uploads, as they can pose security risks. Al
6 min read
PHP | is_uploaded_file( ) Function The is_uploaded_file() function in PHP is an inbuilt function which is used to check whether the specified file uploaded via HTTP POST or not. The name of the file is sent as a parameter to the is_uploaded_file() function and it returns True if the file is uploaded via HTTP POST. This function can b
2 min read
How to upload a file in PHP ? In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart
3 min read
PHP move_uploaded_file() Function The move_uploaded_file() function is an inbuilt function in PHP that is used to change the file destination. This function only works when the file is uploaded by the PHP POST function. If the file is valid it will uploaded. Syntax: move_uploaded_file( string $from, string $to ): boolParameters: Thi
2 min read
How to write Into a File in PHP ? In this article, we are going to discuss how to write into a text file using the PHP built-in fwrite() function. The fwrite() function is used to write into the given file. It stops at the end of the file or when it reaches the specified length passed as a parameter, whichever comes first. The file
2 min read