Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

PHP file uploads

Handle file uploads in PHP: the $_FILES array, upload error codes, validating size and MIME type, move_uploaded_file(), and the php.ini limits that apply.

When a form with enctype="multipart/form-data" posts a file, PHP saves it to a temporary location and describes it in the $_FILES superglobal. Your script must validate the file, then move it to a permanent place with move_uploaded_file() before the request ends, because PHP deletes the temporary file afterwards.

Uploads are a classic attack surface: the file name, the declared type and even the content come from the user. Treat all of it as untrusted.

HTML
<form method="post" enctype="multipart/form-data">
  <input type="file" name="avatar">
  <button>Upload</button>
</form>

Try it

A command-line run has no HTTP request, so there is no real upload. The examples fill $_FILES by hand, in the same shape PHP uses, and run the validation logic you would use on a server. move_uploaded_file() itself works only for real uploads, so the final step is shown read-only.

PHP The shape of $_FILES and error codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Validating size, type and name

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Moving the file (on a server)

Read-only: the final step, in a real request.

PHP
if ($file['error'] === UPLOAD_ERR_OK && is_uploaded_file($file['tmp_name'])) {
    $target = __DIR__ . '/../storage/uploads/' . $safeName;
    if (!move_uploaded_file($file['tmp_name'], $target)) {
        // permissions problem or missing directory
    }
}

Usage notes

  • Check $_FILES[...]['error'] first. UPLOAD_ERR_OK is 0. If it is anything else the other fields are meaningless.
  • Do not trust type or name. The browser sets them. Detect the type with finfo (the fileinfo extension), and for images confirm with getimagesize() or re-encoding with GD.
  • Pick the storage name yourself, from a random value or an id, plus an extension chosen from your allow-list. Using $_FILES['name'] invites path tricks (../) and double extensions (shell.php.png).
  • Store uploads outside the web root when possible, or disable script execution in the upload directory. A PHP file that lands in a public folder can be run.
  • Use move_uploaded_file(), not rename() or copy(): it verifies that the file really came from an HTTP upload.
  • Limits. upload_max_filesize and post_max_size (and max_file_uploads, memory_limit for processing) are set in php.ini, not at runtime; see php.ini. If the whole POST exceeds post_max_size, $_FILES and $_POST arrive empty.
  • Multiple files (name="photos[]") turn each $_FILES field into parallel arrays; loop by index.
  • Protect the form with a CSRF token, and require authentication for who may upload.
  • Scan or re-encode risky types (documents, archives). SVG files can contain scripts, so serve user SVGs with care or not at all.