'How do I send multiple image attachments in html form with php?

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 = '[email protected]';

$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() );
}


Solution 1:[1]

"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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hakre