PHP move_uploaded_file() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, move_uploaded_file() moves a file that was just uploaded through an HTML form from its temporary location to a permanent folder.

It first checks that the file really came from an HTTP upload. This protects your site from a trick where someone tries to make your script move a system file.

Syntax

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

PHP
$ok = move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/photo.jpg');
CallWhat it does
move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/photo.jpg')Saves the uploaded photo into the uploads folder.
move_uploaded_file($tmp, __DIR__ . '/uploads/' . basename($name))Builds a safe destination from the original file name.
move_uploaded_file('/etc/passwd', 'copy.txt')Returns false because that file was not uploaded.

PHP deletes the temporary file when the request ends, so you must call move_uploaded_file() during the same request. Remember to set enctype=multipart/form-data on your HTML form, otherwise $_FILES stays empty.

Library declarations
PHP
move_uploaded_file(string $from, string $to): bool

Parameters

ParameterDescription
fromRequired. The temporary file path, which is stored in $_FILES['field']['tmp_name'].
toRequired. The destination path, including the final file name.

Return Value

Return ValueDescription
boolReturns true on success. It returns false if the file was not uploaded through HTTP POST, or if the move fails.

PHP Examples

Example 1 — Save an Uploaded Photo

Handle a form field named photo and store the file in the uploads folder.

PHP
<?php
if (isset($_FILES['photo']) && $_FILES['photo']['error'] === UPLOAD_ERR_OK) {
    $tmp  = $_FILES['photo']['tmp_name'];
    $dest = __DIR__ . '/uploads/' . basename($_FILES['photo']['name']);

    if (move_uploaded_file($tmp, $dest)) {
        echo "Upload saved as " . basename($dest), PHP_EOL;
    }
}

How It Works

  1. The script first checks that a file arrived and that there was no upload error.
  2. tmp_name is the temporary path where PHP stored the upload.
  3. basename() removes any folder parts from the user's file name for safety.
  4. move_uploaded_file() moves the file and returns true when it works.

Example 2 — A File That Was Not Uploaded

move_uploaded_file() refuses to move any file that did not come from an HTTP upload.

PHP
<?php
$result = move_uploaded_file(__FILE__, __DIR__ . '/copy.php');

var_dump($result);

How It Works

  1. __FILE__ is the path of the running script, which was not uploaded by a browser.
  2. move_uploaded_file() checks the source and refuses to continue.
  3. It returns false and nothing is moved.
  4. This is a safety feature that keeps other files on the server protected.

Example 3 — Allow Only Some File Types and Rename

Check the extension, then store the file under a random name so two users cannot overwrite each other.

PHP
<?php
$allowed = ['jpg', 'png', 'gif'];
$name    = $_FILES['image']['name'];
$tmp     = $_FILES['image']['tmp_name'];
$ext     = strtolower(pathinfo($name, PATHINFO_EXTENSION));

if (!in_array($ext, $allowed, true)) {
    exit("File type not allowed");
}

$newName = bin2hex(random_bytes(8)) . '.' . $ext;

if (is_uploaded_file($tmp) && move_uploaded_file($tmp, __DIR__ . '/uploads/' . $newName)) {
    echo "Saved with a random name ending in .$ext", PHP_EOL;
}

How It Works

  1. pathinfo() reads the extension and strtolower() makes it lowercase.
  2. in_array() with true as the third argument compares the extension strictly with the allowed list.
  3. random_bytes() and bin2hex() build a new unique name, such as 9f3a1c0b5d7e2a44. png.
  4. is_uploaded_file() and move_uploaded_file() make sure the file is real before it is saved.

Frequently Asked Questions

It moves an uploaded file from the temporary folder to a permanent location, after checking that the file came from an HTTP POST upload.
The file may not be an upload, the destination folder may not exist or may not be writable, or the temporary file may already be gone.
The form must use method=post and enctype=multipart/form-data. Also check upload_max_filesize and post_max_size in php.ini.
It is safe against fake source paths, but you must still check file type, size and name yourself. Never save files using the original name without cleaning it.
You can, but they skip the upload check. move_uploaded_file() is the recommended choice for files that came from a form.

Next: parse_ini_file()

parse_ini_file() reads an INI configuration file and returns its settings as an array.

parse_ini_file() 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