我正在尝试从表单发送附件。电子邮件生成的附件不是我试图上传的附件。我希望它在发送给用户时上传并更改名称$strNewFileName
。
html代码:
<input type="file" class="upload" name="uploaded_file[]" id="uploaded_file"></input>
php文件:
<?php
$strTo = "me@example.com";
$strSubject = "Document Updated";
$strMessage = nl2br($_GET["sqlCode"]);
$strNewFileName = $_GET["RevisedFileName"];
if (is_uploaded_file($_FILES['uploaded_file']['tmp_name'])) {
echo "File ". $_FILES['uploaded_file']['name'] ." uploaded successfully.\n";
echo "Displaying contents\n";
readfile($_FILES['uploaded_file']['tmp_name']);
} else {
echo "Possible file upload attack: ";
echo "filename '". $_FILES['uploaded_file']['tmp_name'] . "'.";
}
//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));
$strHeader = "";
$strHeader .= "From: Document Update\r\nReply-To: example@example.com";
$strHeader .= "MIME-Version: 1.0\n";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"\n\n";
$strHeader .= "This is a multi-part message in MIME format.\n";
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-type: text/html; charset=utf-8\n";
$strHeader .= "Content-Transfer-Encoding: 7bit\n\n";
$strHeader .= $strMessage."\n\n";
rename("$strFilesName", "$strNewFileName");
// *** Attachment *** //
$strFilesName = $_FILES["uploaded_file"]["name"];
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["uploaded_file"]["name"])));
$strHeader .= "--".$strSid."\n";
$strHeader .= "Content-Type: application/pdf; name=\"".$strNewFileName."\"\r\n";
$strHeader .= "Content-Transfer-Encoding: base64\n";
$strHeader .= "Content-Disposition: attachment;\n\n";
$strHeader .= $strContent."\n\n";
$flgSend = @mail($strTo,$strSubject,$strMessage,$strHeader);
if($flgSend)
{
echo "Approval Send Complete.";
}
else
{
echo "Cannot send mail, Approval has not been processed.";
}
?>
答案 0 :(得分:0)
看起来您已将文件上传设置为发送多个文件name="uploaded_file[]"
,但您只需处理php代码中的单个文件。在这种情况下,只需删除html中的[]
。
答案 1 :(得分:0)
我认为你的问题在这里:
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["uploaded_file"]["name"])));
_FILES["uploaded_file"]["name"]
表示上传文件的原始名称,而不是服务器上的位置。您需要使用$_FILES['uploaded_file']['tmp_name']
(正如您之前在验证它的脚本中所做的那样是正确上传的文件。
同样对于数组访问(因为使用uploaded_file[]
作为输入名称),您需要执行类似这样的操作来构建有效附件数组:
$valid_attachments = array();
foreach ($_FILES['uploaded_file']['tmp_name'] as $key => $temp_file_name) {
if (is_uploaded_file($temp_file_name)) {
$valid_attachments[] = array(
'path' => $temp_file_name,
'filename' => $_FILES['uploaded_file']['name'][$key],
'type' => $_FILES['uploaded_file']['type'][$key],
'size' => $_FILES['uploaded_file']['size'][$key]
);
}
}
然后,您需要在代码的“附件”部分中遍历此数组,并将附件添加到电子邮件中。