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>
<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>";
}
?>
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>";
}
?>
0 Comments