Open In App

Define multipart form data

Improve
Improve
Like Article
Like
Save
Share
Report

The encoding process is performed before data is sent to the server as spaces are converted to (+) symbol and non-alphanumeric characters or special characters are converted to hexadecimal (0-9, A-F) values as the ASCII character set is the format for sending data on the Internet. So, the real purpose of encoding is to make the data in a standard format so that it can be sent on the Internet.

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.  

Syntax:

<form action="login.php" method="post" 
    enctype="multipart/form-data">
</form>

Approach: In this example, we are creating a simple HTML form (uploading.html) for uploading an image file to the server. The server will process the POST request of HTML form using uploading.php file. The uploading code will upload the file to the folder “upload-images” and will be displayed on the browser as well.

uploading.htm




<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>Multipart Form Data</title>
  </head>
  <body>
    <form action="uploading.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>


uploading.php




<?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:



Last Updated : 01 Feb, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads