Multiple File Upload in PHP | Sanwebcorner

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.


index.php

<html>
<head>
<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){
$("#sanForm").on('submit',(function(e){
e.preventDefault();
$('#loader-icon').show();
var valid;
valid = validateContact();
if(valid) {
$.ajax({
url: "mail-send.php",
type: "POST",
data:  new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#mail-status").html(data);
$('#loader-icon').hide();
},
error: function(){}      

});
}
}));

function validateContact() {
var valid = true;
$(".formfield").css('background-color','');
$(".info").html('');
$("#u_name").removeClass("invalid");
$("#u_email").removeClass("invalid");
$("#subject").removeClass("invalid");
$("#comment").removeClass("invalid");

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

return valid;
}

});
</script>
</head>
<body>
<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>
    <div id="mail-status"></div>
    <div>
        <input
            type="text" name="u_name" id="u_name"
            class="formfield" placeholder="Name">
    </div>
    <div>
        <input type="text" name="u_email" id="u_email"
            class="formfield" placeholder="Email">
    </div>
    <div>
        <input type="text" name="subject" id="subject"
            class="formfield" placeholder="Subject">
    </div>
    <div>
        <textarea name="comment" id="comment" class="formfield"
            cols="60" rows="6" placeholder="comment"></textarea>
    </div>
    <div>
        <label>Attachment</label><br /> <input type="file"
            name="attachment[]" class="formfield" multiple="multiple">
    </div>
    <div>
        <input type="submit" value="Send" class="san_button" />
    </div>
</form>
</body>
</html>

mail-send.php

<?php
require('phpmailer/class.phpmailer.php');
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='';
$message.="Name  : ".$_POST['u_name']." <br/>";
//$message='';
$message.="Email  : ".$_POST['u_email']." <br/>";
$message.="Subject  : ".$_POST['subject']." <br/>";
$message.="Email  : ".$_POST['comment']." <br/>";
$mail->MsgHTML($message);

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

$mail->IsHTML(true);
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

Labels

360 degree view product image 3d rotate 3d text 3step-form Accordion menu ajax All Browsers Animated Background Gradient animation audio controller auto complete Best accordion menu bootstrap border bounce effect on hover Breadcrumb calculate age by dob calendar Captcha change image on mouse move color chooser countdown Counter on scroll create random number Css css animation css buttons css hover css3 customized upload Button Date picker delete data from table Different scroll speed email otp verification email verifier Embed eventlist Expanding Menu feedbck form flash news section close button full screen menu Google custom search Google Maps Google reCAPTCHA Google site search gradient hide menu Horizontal News ticker Hover Htm Html html dropdown html form html5 html5 form validation Iframe image hover increment decrement textbox value based on select box Javascript Jquery js Layered image effect on mouse hover lock & unlock page scroll marquee marquee with close button mobile number validation mobile otp login Multi-level-drop-down-menu multi-level-form multi-select-checkbox Multi-Step-form Multiple File Upload in PHP mysql News ticker on hover change text to image our team page password generator password strength checker Photoshop PHP php mailer populate html selectbox Popup image on click the link pure css Question type form random number generate Random password Redirect page Based on the select box while submit the form responsive responsive menu responsive popup box Responsive Vertical Time line design rotate image selectbox based on select box simple coming soon page simple countdown based on end date simple typing effect slider sliding tab sms otp login statistic counter on scroll switch tag shape team member Time picker toggle toggle view transparent button underline animation up down slide validation vertical fixed sidebar Webtips whatsapp link website Youtube