1

This form handles the form data and a single image nicely.

However... when I add the brackets [] to the file input name to handle multiple images, I receive no images at all in the receiving email. I just receive a blank 'alternative' that displays "Array".

Please advise me what I am missing in this code.


HTML:

<!DOCTYPE html>

<head>

<title>My Form</title>

</head>

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

<input type="text" id="userName" name="userName" placeholder=" Enter 
Your Full Name"><br><br>
<input type="text" id="userEmail" name="userEmail" placeholder=" Enter 
Your Emal Address"><br><br><br>

<input type="file" id="file" name="userImages" multiple /><br><br><br>

<input type="submit" id="submitButton" name="submitButton" value="Submit 
Form">

</form>

</html>

PHP:

<?php

$filenameee =  $_FILES['userImages']['name'];
$fileName = $_FILES['userImages']['tmp_name']; 

$name = $_POST['userName'];
$email = $_POST['userEmail'];

$composition =

"\r\nName: ". $name .
"\r\nEmail Address: " . $email;

$subject ="Email Subject Line";

$fromname ="$name";
$fromemail = "$email";

$mailto = 'myaddress@email.com';

$content = file_get_contents($fileName);
$content = chunk_split(base64_encode($content));

$separator = md5(time());

$eol = "\r\n";

$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 . "--";

if (mail($mailto, $subject, $body, $headers)) {
echo "Thank you for submitting your form."; // The message the user will see after their form 
has sent

} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
  • you'll need to iterate through the entire collection of files and add each one to the message body - ensuring that you respect the boundary for each one. – Professor Abronsius May 13 '22 at 22:36
  • @HeatherLynn https://stackoverflow.com/questions/2704314/multiple-file-upload-in-php This answer has some good insight in how to handle multi-file upload. Also there appears to be an issue with the `$composition` variable in your code, you need a semi-colon after `$email`. – Jacob Mulquin May 13 '22 at 22:50
  • Thank you Professor Abronsius and hakre. And yes, Jacob... I had already caught composition variable. Thank you. I will dig a little deeper and hopefully fix my issue. –  May 13 '22 at 23:16
  • Is it a requirement that the user sends / uploads at least one image or is that optional? – Professor Abronsius May 14 '22 at 10:42

1 Answers1

0

"Array" - that is an array converted to string (e.g. .). check the php docs for files form with multiple files. the structure of the $_FILES array differs from with a single file.

Error is likely here (and then all similar places):

$filenameee =  $_FILES['userImages']['name'];

$filenameee is an array if there are multiple files.

# first file
$filenameee =  $_FILES['userImages']['name'][0];
# second file
$filenameee =  $_FILES['userImages']['name'][1];

Compare: Uploading multiple files

hakre
  • 193,403
  • 52
  • 435
  • 836