PHP is_uploaded_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, is_uploaded_file() tells you whether a file was really uploaded through an HTTP POST form. It protects your code from fake file paths.

When a visitor uploads a file, PHP stores it in a temporary place and gives you the path in $_FILES. is_uploaded_file() returns true only for those genuine temporary files, so a cheater cannot trick your script into using a system file.

Syntax

Call is_uploaded_file() with the values you need. Start with this line.

PHP
$ok = is_uploaded_file($_FILES['photo']['tmp_name']);
CallWhat it does
is_uploaded_file($_FILES['photo']['tmp_name'])Returns true if that path is a genuine uploaded file.
is_uploaded_file('/etc/passwd')Returns false because this file was not uploaded.
is_uploaded_file(__FILE__)Returns false because the script itself was not uploaded.

The function is meant to be used with the tmp_name value from $_FILES. It is usually followed by move_uploaded_file(), which does the same check again and then moves the file to its final place.

Library declarations
PHP
is_uploaded_file(string $filename): bool

Parameters

ParameterDescription
filenameRequired. The temporary file name, which is the tmp_name value of an entry in $_FILES.

Return Value

Return ValueDescription
boolReturns true if the file was uploaded using HTTP POST, otherwise false.

PHP Examples

Example 1 — Confirm a Genuine Upload

Assume an HTML form with a file field named photo was submitted with the POST method and enctype multipart/form-data.

PHP
<?php
if (isset($_FILES['photo']) && is_uploaded_file($_FILES['photo']['tmp_name'])) {
    echo "Upload is valid", PHP_EOL;
} else {
    echo "No valid upload", PHP_EOL;
}

How It Works

  1. isset() makes sure the form really sent a field named photo.
  2. $_FILES['photo']['tmp_name'] holds the temporary path chosen by PHP.
  3. is_uploaded_file() returns true because the file came from an HTTP POST upload.
  4. If nothing was uploaded, the else branch prints No valid upload.

Example 2 — A Normal File Is Not an Upload

Only files that came through a form upload are accepted. A path to any other file returns false, even if the file exists.

PHP
<?php
if (is_uploaded_file(__FILE__)) {
    echo "This script is an uploaded file", PHP_EOL;
} else {
    echo "This script is not an uploaded file", PHP_EOL;
}

How It Works

  1. __FILE__ is the path of the running script, which exists on disk.
  2. PHP did not receive it through a POST upload, so is_uploaded_file() returns false.
  3. This is the reason the function blocks attackers who send a path like /etc/passwd as the file name.
  4. The else branch prints the message.

Example 3 — Validate and Save an Upload

A safe upload handler checks the error code, confirms the upload with is_uploaded_file(), and then moves the file. Assume a user uploaded a file named avatar.png.

PHP
<?php
$file = $_FILES['avatar'] ?? null;

if ($file === null || $file['error'] !== UPLOAD_ERR_OK) {
    echo "Upload error", PHP_EOL;
} elseif (!is_uploaded_file($file['tmp_name'])) {
    echo "Not a real upload", PHP_EOL;
} else {
    $folder = __DIR__ . '/uploads';
    if (!is_dir($folder)) {
        mkdir($folder, 0755, true);
    }
    $name = basename($file['name']);

    if (move_uploaded_file($file['tmp_name'], $folder . '/' . $name)) {
        echo "Saved as uploads/", $name, PHP_EOL;
    }
}

How It Works

  1. The first check stops if the field is missing or PHP reports an upload error.
  2. is_uploaded_file() makes sure tmp_name is a genuine temporary upload.
  3. is_dir() and mkdir() create the uploads folder when it does not exist yet.
  4. basename() removes any folder parts from the user's file name, and move_uploaded_file() saves the file.

Frequently Asked Questions

It checks whether a file was uploaded through an HTTP POST request. It returns true only for real temporary upload files.
It stops attackers from sending a path to a private file, such as a system file, as if it were an upload. Only genuine uploads pass the check.
is_uploaded_file() only checks the file. move_uploaded_file() checks it too and then moves it to a new place. Using move_uploaded_file() alone is often enough.
Pass $_FILES['field']['tmp_name'], which is the temporary path. Do not pass the original file name sent by the browser.
No. Also check the error code, the file size, the real file type and use a safe file name. Store uploads outside folders that can run PHP code when possible.

Next: is_writable()

is_writable() tells you whether a file or folder exists and PHP is allowed to write to it.

is_writable() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful