move_uploaded_file()

move_uploaded_file() — Moves an uploaded file to a new location.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

This function checks to ensure that the file designated by from is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by to.

Signature

move_uploaded_file(string $from, string $to): bool

Parameters

ParameterTypeWhat it takes
$fromstringThe filename of the uploaded file.
$tostringThe destination of the moved file.

Return value

Returns true on success. If from is not a valid upload file, then no action will occur, and move_uploaded_file() will return false. If from is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return false. Additionally, a warning will be issued.

Example

A runnable snippet:

$uploads_dir = '/uploads'; foreach ($_FILES["pictures"]["error"] as $key => $error) { if ($error == UPLOAD_ERR_OK) { $tmp_name = $_FILES["pictures"]["tmp_name"][$key]; // basename() may prevent filesystem traversal attacks; // further validation/sanitation of the filename may be appropriate $name = basename($_FILES["pictures"]["name"][$key]); move_uploaded_file($tmp_name, "$uploads_dir/$name"); } }

See also

is_uploaded_file(), rename()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page move_uploaded_file() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.