簡體   English   中英

如何使用 php 以 html 形式發送多個圖像附件?

[英]How do I send multiple image attachments in html form with php?

此表單很好地處理表單數據和單個圖像。

但是......當我將括號 [] 添加到文件輸入名稱以處理多個圖像時,我在接收電子郵件中根本沒有收到任何圖像。 我剛剛收到一個顯示“數組”的空白“替代”。

請告訴我我在這段代碼中缺少什么。


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

"Array" - 即轉換為字符串的數組(例如. )。 檢查 php 文檔以獲取具有多個文件的文件形式。 $_FILES數組的結構不同於單個文件。

錯誤可能在這里(然后是所有類似的地方):

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

如果有多個文件, $filenameee是一個數組。

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

比較:上傳多個文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM