Reputation:
I have a form on my site which requires users to enter data in a few basic fields, and then upload multiple image files to submit with their form.
The 'form' side of the equation seems easy enough, by simply adding multiple="multiple" to the input field.
However... after many attempts at different variations of code, I am still not understanding (or able) to get the php 'formHandler' side of the equation to actually send multiple image files.
Toying with the code got really confusing, and I made an awful mess of it.
So....
For this example, I left the 'multiple' aspect in the form input, however... to clean up my mess... I stripped back the php 'formHandler' code to where I had it before, which works fine for sending just a single image file.
Any advice as to modify the php 'formHandler' to properly send multiple images would be a life-saver and much appreciated.
Thanks in advance!
The Form:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
</head>
<body>
<h1>The Form</h1>
<form action="/formHandler.php" method="post" enctype="multipart/form-data">
<input type="text" id="userName" name="userName" class="inputField" placeholder=" Enter Your
Name" required><br><br>
<input type="email" id="userEmail" name="userEmail" class="inputField" placeholder=" Enter
Your Email Address" required><br><br><br>
<!-- Start Image Upload Section -->
<a>Select Multiple Images:</a><br><br>
<input type="file" id="file" name="userImages[]" multiple="multiple" required/><br><br><br><br>
<!-- End Image Upload Section -->
<input type="submit" id="submitButton" name="submitButton" value="Submit Form">
</body>
</html>
The php 'formHandler':
<?php
////////////////////////////////////////////////////////////////////////////
$filenameee = $_FILES['userImages']['name'];
$fileName = $_FILES['userImages']['tmp_name'];
$name = $_POST['userName'];
$email = $_POST['userEmail'];
////////////////////////////////////////////////////////////////////////////
$composition =
"\r\n\n\nName: " . $name .
"\r\nEmail Address: " . $email;
////////////////////////////////////////////////////////////////////////////
//Whatever you want to subject of the email to be when you receive the form.
$subject ="Form Submission";
////////////////////////////////////////////////////////////////////////////
//User name and email will be displayed in header when form is received.
$fromname ="$name";
$fromemail = "$email";
////////////////////////////////////////////////////////////////////////////
//The email address at which you want to receive the form.
$mailto = '[email protected]';
////////////////////////////////////////////////////////////////////////////
//The Content Action
$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));
//A random hash will be necessary to send mixed content.
$separator = md5(time());
//Carriage return type (RFC)
$eol = "\r\n";
//Main header (multipart mandatory)
$headers = "From: ".$fromname." <".$fromemail.">" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $composition . $eol;
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $filenameee . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
////////////////////////////////////////////////////////////////////////////
//Mail send action
@mail($mailto, $subject, $body, $headers);
////////////////////////////////////////////////////////////////////////////
//Page Re-direct after form submission
header('location: /formSent_confirmationPage.php');
?>
Upvotes: 0
Views: 401
Reputation: 562
Change input file name to userImages[] that allows to populate an array and iterate through $_files element ($filename is now an array). https://www.php.net/manual/en/features.file-upload.multiple.php
$filenameee = $_FILES['userImages']['name'];
$sizes = $_FILES['userImages']['size'];
$fileName = $_FILES['userImages']['tmp_name'];
foreach ($fileName as $tmpname) {
echo "<br> Tmp name: " . $tmpname;
}
foreach ($filenameee as $name) {
echo "<br> Chosen user name : " . $name;
}
foreach ($sizes as $size) {
echo "<br> File size : " . $size/1024 . " Ko";
}
Upvotes: 1