Sometimes we want to upload a file in our HTML form. PHP provides the feature for handling a file at server side with $_FILES array. We can also check different properties of an files like file-name, file-type, file-size, and some other attributes.
Read Also: How to Upload File in PHP
Index
PHP provides one global variable called $_FILES. It is a two-dimensional associative array that keeps information related to uploaded files via the HTTP POST method.
It holds attribute as follows:
Attribute | Description |
[name] | Name of the file being uploaded |
[size] | Size of the file |
[type] | Type of file |
[tmp_name] | Temporary address where the file is located before processing the upload request |
[error] | Error code associated with the file |
Above attributes syntax with $_FILES.
Lets, take a straightforward example that indicates different $_FILES attributes.
<?php echo "<pre>"; print_r($_FILES); echo "</pre>"; ?> <form action-xhr="#" method="post" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" value="Upload Image"> </form>
The above code displays the following HTML page to users.
We select the file and upload it then print_r displays all information regarding the data. Suppose we upload download.png file then it will display the output as follows:
Output:
Array (
[file] => Array
(
[name] => errorsea.txt
[type] => Text Document (.txt)
[tmp_name] => 5EB63BBBE01EEED093CB22BB8F5ACDC3
[size] => 69696
[error] => 0
)
)
Suppose we want to create a form that takes an image file and upload it on the server.
<!DOCTYPE html> <html> <head> <title>Errorsea</title> </head> <body> <form action="upload.php" enctype="multipart/form-data" method="post"> <h2>Upload file</h2> <input type="file" name="pic"> <input type="submit" name="submit" value="Uplaod file"><br><br> // Here file name is used for accessing the file </form> </body> </html>
<?php if(isset($_POST['submit'])) { if(isset($_FILES["pic"]) && $_FILES["pic"]["error"] == 0) { $name = $_FILES['pic']['name']; $size = $_FILES['pic']['size']; $type = $_FILES['pic']['type']; $temp_name = $_FILES['pic']['tmp_name']; $error = $_FILES['pic']['error']; echo "<div style='padding:15px;'> File Properties </div>"; echo "<div style='padding:15px;'>Name : ".$name."</div>"; echo "<div style='padding:15px;'>Size : ".$size."</div>"; echo "<div style='padding:15px;'>Type : ".$type."</div>"; echo "<div style='padding:15px;'>Temporary Name : ".$temp_name."</div>"; echo "<div style='padding:15px;'>Errors : ".$error."</div>"; } } ?>
Once the file is uploaded successfully, then we can access all file attributes from $_FILES array. We can access file type, file size, and file name, etc.
Output:
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…