What is PHP $_FILES constant?

Share

In PHP web development, there are constants that you can define to have a global scope. PHP similarly has constants that are built-in and also have a global scope. One such constant is the $_FILES constant, which is used in file management.

What is $_FILES in PHP?

$_FILES is a global constant or predefined variable in PHP that can be used to associate array items that are uploaded through the HTTP POST method.

When you write your form to upload your file, remember to always set the value of the enctype form attribute as multipart\form-data.

Properties

The file in the properties listed below is the name of the variable that contains the file.

  • $_FILES['file']['name']: This is the name of the file to be uploaded originally.

  • $_FILES['file']['type']: This holds the mime type of the file.

  • $_FILES['file']['size']: This is the size of the file to be uploaded in bytes.

  • $_FILES['file']['tmp_name']: This will include the temporary filename of the file in which the uploaded file was stored on the server.

  • $_FILES['file']['error']: Any error code associated with the file upload will be saved here.

Let’s look at how the $_files global constant is used to handle a file upload in PHP.

Code

<form action="testscript.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type ="submit" value="submit">
</form>
<?php
// testing all properties of the $_FILES
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>";
//Below is a script used to upload file in php
// with the help of $_FILES constant
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>

Explanation

This code enables you to upload files in your code using the features of the $_FILES global constant. It is a bulky, complete code set. Below, the code is explained block by block.

  • The first four lines of the code indicate the HTML form that can be used to submit the files. This code can be added in the PHP code where the form action will point to the script itself or just left blank. But, if added to an external file, the action attribute should be given a value which should be the name of the PHP script that should be executed when the submit button is hit.

  • In our PHP script, the code in line 8 to line 12 will use the $_FILE() global constant to get the details about the file that the form submitted to the script.

  • From line 18 to line 21 we declare some variables, like the $target_dir indicates the path to the folder where the uploaded file will be saved. The $target_file variable will get the fully qualified name of the file when uploaded to the $target_dir, and something like uploads/birds.png can be obtained from that line. $imageFileType will get the file extension of the upload; it can be .jpg, .png, gif, txt and so on.

  • The rest of the script, starting from line 24 down to the end of the scripts, contains blocks of different if statements. They are all geared towards carrying out some error check and displaying the right error messages in order to finally save the uploaded files barring any errors. The errors checked include:

    • Checking that the file is actually an image, because in this example the target file to be uploaded is an image file.

    • Checking if the file exists already in the target directory.

    • Checking and restricting size of file.

    • Checking that the extension is an allowed extension.

    • Communicating the upload status to the user.