Multiple File Upload in PHP

In this article we will see how to upload multiple files in php. It is possible to upload multiple files in single input upload field using html and php. After upload will send the data and uploaded files to the particular email id using the php mailer. First you need to create html form with the required fields and you should add the enctype="multipart/form-data" to the form to accept upload and add post method to form.

Then set action to "mail-send.php" file. Here you can write the code to send the form fields data to mail using php mailer. Here you can set the loop for multiple attachments. This example is working fine and i given complete code to your reference and also i given the demo link for this example. Click the demo link and check it out the result of this example.


<title>Upload multiple files in php form at a time</title>
<link rel="stylesheet" type="text/css" href="swc.css">
<script src="jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function (e){
var valid;
valid = validateContact();
if(valid) {
url: "mail-send.php",
type: "POST",
data:  new FormData(this),
contentType: false,
cache: false,
success: function(data){
error: function(){}      


function validateContact() {
var valid = true;

if(!$("#u_name").val()) {
        valid = false;
    if(!$("#u_email").val()) {
        valid = false;
    if(!$("#u_email").val().match(/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/)) {
        $("#u_email").attr("title","Invalid Email");
        valid = false;
if(!$("#subject").val()) {
valid = false;
if(!$("#comment").val()) {
valid = false;

return valid;

<form id="sanForm" action="" method="post" enctype='multipart/form-data'>
<div id="loader-icon" style="display: none;">
    <img src="ajax-loading-icon.gif" style="width: 85px;"/>
    <div id="mail-status"></div>
            type="text" name="u_name" id="u_name"
            class="formfield" placeholder="Name">
        <input type="text" name="u_email" id="u_email"
            class="formfield" placeholder="Email">
        <input type="text" name="subject" id="subject"
            class="formfield" placeholder="Subject">
        <textarea name="comment" id="comment" class="formfield"
            cols="60" rows="6" placeholder="comment"></textarea>
        <label>Attachment</label><br /> <input type="file"
            name="attachment[]" class="formfield" multiple="multiple">
        <input type="submit" value="Send" class="san_button" />


require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->Mailer = "mail";
$mail->SMTPDebug = 1;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port     = 587;
$mail->Username = "Your username";
$mail->Password = "Your password";
$mail->Host     = "Your Host";
$mail->Mailer   = "smtp";
$mail->SetFrom("From Email", "website");
$mail->AddReplyTo($_POST["u_email"], $_POST["u_name"]);
$mail->AddAddress("To Email");
$mail->Subject = $_POST["subject"];
$mail->WordWrap   = 80;
$message.="Name  : ".$_POST['u_name']." <br/>";
$message.="Email  : ".$_POST['u_email']." <br/>";
$message.="Subject  : ".$_POST['subject']." <br/>";
$message.="Email  : ".$_POST['comment']." <br/>";

foreach ($_FILES["attachment"]["name"] as $k => $v) {
    $mail->AddAttachment( $_FILES["attachment"]["tmp_name"][$k], $_FILES["attachment"]["name"][$k] );

if(!$mail->Send()) {
echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
echo "<p class='success'>Mail Sent Successfully.</p>";

Multi File upload result screenshot:

Post a comment