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.
<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.
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.
Moving the file (on a server)
Read-only: the final step, in a real request.
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_OKis0. If it is anything else the other fields are meaningless. - Do not trust
typeorname. The browser sets them. Detect the type withfinfo(thefileinfoextension), and for images confirm withgetimagesize()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(), notrename()orcopy(): it verifies that the file really came from an HTTP upload. - Limits.
upload_max_filesizeandpost_max_size(andmax_file_uploads,memory_limitfor processing) are set inphp.ini, not at runtime; see php.ini. If the whole POST exceedspost_max_size,$_FILESand$_POSTarrive empty. - Multiple files (
name="photos[]") turn each$_FILESfield 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.