Skip to content

PHP Tutorial Superglobals

$_FILES

Lesson 152 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

When a form uploads a file, the file does not go into $_POST. PHP stores it in a temporary location and describes it in $_FILES.

A form that uploads a file needs two things:

index.html
HTML
<form action="upload.php" method="post" enctype="multipart/form-data">
  <label for="photo">Photo</label>
  <input type="file" id="photo" name="photo">
  <button type="submit">Upload</button>
</form>

enctype="multipart/form-data" and method="post" are both required.

What $_FILES looks like

For a field named photo, PHP gives you five values.

index.php
PHP
<?php
$_FILES = [
    "photo" => [
        "name" => "holiday.jpg",
        "type" => "image/jpeg",
        "tmp_name" => "/tmp/phpA1B2C3",
        "error" => 0,
        "size" => 482133,
    ],
];

$file = $_FILES["photo"];

echo $file["name"] . " is " . round($file["size"] / 1024) . " KB";
Output
OUTPUT
holiday.jpg is 471 KB
KeyMeaningTrust it?
nameOriginal file nameNo, chosen by the visitor
typeMIME typeNo, sent by the browser
tmp_nameWhere PHP saved the uploadYes, set by PHP
errorUpload status codeYes
sizeSize in bytesMostly (still check the real file)

Check the error code first

CodeConstantMeaning
0UPLOAD_ERR_OKSuccess
1UPLOAD_ERR_INI_SIZELarger than upload_max_filesize
2UPLOAD_ERR_FORM_SIZELarger than the form’s MAX_FILE_SIZE
3UPLOAD_ERR_PARTIALOnly partly uploaded
4UPLOAD_ERR_NO_FILENo file was chosen
6UPLOAD_ERR_NO_TMP_DIRMissing temporary folder
7UPLOAD_ERR_CANT_WRITECould not write to disk
index.php
PHP
<?php
$_FILES = ["photo" => ["name" => "", "error" => UPLOAD_ERR_NO_FILE]];

if (($_FILES["photo"]["error"] ?? UPLOAD_ERR_NO_FILE) !== UPLOAD_ERR_OK) {
    echo "No file was uploaded correctly.";
}
Output
OUTPUT
No file was uploaded correctly.

The golden rule

Never trust name or type. They are just text sent by the visitor. A file called photo.jpg can contain PHP code. The next chapter shows how to check and store uploads properly: File Uploads and Secure File Uploads.

dangerous
Limits live in php.ini
upload_max_filesize and post_max_size set the size limits. If a large upload fails with no message, those are the first settings to check.