Define Multipart Form Data
Multipart form data is a way for a web form to send files and other information to a server. When you upload a file, the form needs to send text and the actual file data (like pictures, documents, etc.).
Instead of putting everything into one long string, multipart form data breaks the form into parts. Each part contains one piece of information, like a text field or a file, and they are separated by special markers called boundaries.
Syntax:
<form action="login.php" method="post"
enctype="multipart/form-data">
</form>
In this syntax:
<form>
: This tag creates a form on a webpage where users can enter data.action="login.php"
: When the form is submitted, the data is sent to the filelogin.php
on the server for processing.method="post"
: The form data is sent using the POST method, which allows sending larger amounts of data securely (like files or passwords).enctype="multipart/form-data"
: This tells the browser to send the form data in multiple parts, which is necessary when the form includes file uploads.
Example: Image Upload Form Using HTML and PHP
In this example, we are creating a basic HTML form that allows users to upload an image file to the server. The server will handle the form's POST request using the uploading.php script. This PHP code will upload the file to a designated folder called "upload-images" and display the uploaded image on the browser.
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="POST"
enctype="multipart/form-data">
<h4>Browse your file</h4>
<input type="file" name="uploadfile" /> <br><br>
<input type="submit" value="Upload File" />
</form>
</body>
</html>
<?php
error_reporting(0);
$filename = $_FILES["uploadfile"]["name"];
$tempname = $_FILES["uploadfile"]["tmp_name"];
$folder = "upload-images/".$filename;
move_uploaded_file($tempname,$folder);
echo "<img src='$folder' height=100 width=100 />";
?>
Output:

In this example:
- The HTML form lets users select a file to upload using
<input type="file">
and sends it toupload.php
using POST method withmultipart/form-data
encoding. - In
upload.php
, PHP retrieves the uploaded file’s original name and temporary file location from the$_FILES
array. - The script defines a folder path (
upload-images/
) and appends the uploaded file’s name to create the destination path. - The
move_uploaded_file
function moves the file from the temporary location to the target folder on the server. - Finally, the uploaded image is displayed on the webpage with a fixed size of 100x100 pixels.