PHP Reference ยท Variables and Constants
PHP $_FILES superglobal
Reference for PHP $_FILES: the structure of an uploaded file entry, error codes, multiple uploads, and the checks you need before you move a file.
$_FILES describes files uploaded through a form with method="post" and enctype="multipart/form-data". For every file input, PHP stores the upload in a temporary location and adds an entry with the original name, the claimed type, the size, the temporary path and an error code.
PHP deletes the temporary file when the script ends, so you must move it to its final place with move_uploaded_file(). Everything in the entry except tmp_name and size comes from the client and cannot be trusted.
<?php $file = $_FILES["avatar"] ?? null; if ($file && $file["error"] === UPLOAD_ERR_OK) { move_uploaded_file($file["tmp_name"], "/var/uploads/avatar.png"); }
Try it
There is no browser here, so the examples build the same arrays PHP would. move_uploaded_file() refuses files that did not arrive through an HTTP upload, so the examples only validate and name the files.
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.
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.
Syntax
$_FILES["field"]["name"] $_FILES["field"]["type"] $_FILES["field"]["tmp_name"] $_FILES["field"]["error"] $_FILES["field"]["size"] move_uploaded_file(string $from, string $to): bool is_uploaded_file(string $filename): bool
| Key | Description |
|---|---|
name |
Original file name on the client's machine. Client-controlled. |
type |
MIME type claimed by the browser. Client-controlled. |
tmp_name |
Path of the temporary file on the server. |
error |
An UPLOAD_ERR_* constant. 0 (UPLOAD_ERR_OK) means success. |
size |
Size in bytes, measured by PHP. |
full_path |
(PHP 8.1) The relative path sent by directory uploads. Client-controlled. |
Error codes: UPLOAD_ERR_OK 0, INI_SIZE 1, FORM_SIZE 2, PARTIAL 3, NO_FILE 4, NO_TMP_DIR 6, CANT_WRITE 7, EXTENSION 8 (there is no 5).
Usage notes
- Form requirements. The form needs
method="post"andenctype="multipart/form-data"; without the encoding only the file name arrives, as a text field. - Check
errorfirst.UPLOAD_ERR_NO_FILEis normal for an optional field. Only continue when the code isUPLOAD_ERR_OK. - Verify the real content. The
typefield is only a claim. Check the file withfinfo(new finfo(FILEINFO_MIME_TYPE)) orgetimagesize()for images, and compare against an allow list. - Never trust
name. It may contain../or.php. Generate your own file name, keep only an extension from an allow list, and store uploads outside the web root or in a folder where scripts do not run. - Use
move_uploaded_file(). It checks that the file really came from an upload.is_uploaded_file()does the same check without moving. - Size limits come from
upload_max_filesize,post_max_size,max_file_uploads(default 20) and sometimes a hiddenMAX_FILE_SIZEfield (a browser hint only). If the whole body exceedspost_max_size, both$_POSTand$_FILESare empty. - Multiple files use
name="photos[]", and PHP lays the data out by property, not by file. Reorganize it as shown above. - Temporary files are deleted when the script finishes, so move or copy them first.
- Check the destination.
move_uploaded_file()returnsfalseif the target folder is missing or not writable. - Command line.
$_FILESis empty in the CLI andmove_uploaded_file()always fails there.
Related
- File uploads,
$_POST,file_put_contents(),$_SERVER - Lessons: File uploads, Forms, Secure coding