Back to results for “Security”
PHP › File Upload Handling

How does PHP handle file uploads submitted through an HTML form, and what information does the $_FILES superglobal provide?

Beginner

Answer

When a user submits a form containing a file input with the proper multipart encoding, PHP automatically stores information about the uploaded file in the $_FILES superglobal, including the file's original name, its temporary server side location, its size, and its MIME type, along with an error code indicating whether the upload succeeded, giving your script everything it needs to validate and then move the uploaded file to a permanent location.
<form method="post" enctype="multipart/form-data">
  <input type="file" name="document">
</form>

$fileName = $_FILES['document']['name'];
$tmpPath = $_FILES['document']['tmp_name'];
Real-world example A document upload feature reads the temporary file path and original file name from the $_FILES superglobal immediately after a form submission, using that information to decide where and under what name to permanently store the uploaded file.

Common follow-ups: Why does the form need the enctype multipart form data attribute for file uploads to work?;What does each possible value of the upload error code mean?