2

我正在尝试从表单发送附件。该电子邮件会生成一个附件,而不是我尝试上传的附件。我希望它上传并将名称更改为$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.";  
 } 

 ?>
4

2 回答 2

0

看起来您将文件上传设置为发送多个文件,name="uploaded_file[]"但您只在 php 代码中处理单个文件。在这种情况下,只需删除[]html 中的 。

于 2013-02-21T17:48:35.187 回答
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]
        );
    }
}

然后,您需要在代码的“附件”部分中遍历此数组并将附件添加到电子邮件中。

于 2013-02-21T17:49:40.073 回答