0

您好,我正在通过电子邮件提交 html 表单。服务器端脚本是 PHP 。我的 html 表单是

<form action='sendmail.php' method='post'>
   Name:<input type="text" name="name">
    File:<input type='file' name='attach'>
<input type="submit">

我用于发送电子邮件的 php 文件是

<?php 
   $strTo = "xyz@gmail.com";
     $strHeader .= "From: ".$_POST["name"]."<".$_POST["name"].">";  
     $strHeader .= "MIME-Version: 1.0\n";
     $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";  
 if($_FILES["attach"]["name"] != "")
 {
        $strFilesName = $_FILES["fileAttach"]["name"];  
        $strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));  
       $strHeader .= "--".$strSid."\n";  
         $strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"\n";  
       $strHeader .= "Content-Transfer-Encoding: base64\n";  
        $strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"\n\n";  
       $strHeader .= $strContent."\n\n";  
       }
 $flgSend = @mail($strTo,"no-subjet",null,$strHeader); 

但它不会进入 if 条件

4

1 回答 1

7

尝试用这个替换你的表单:

<form action="sendmail.php" method="post" enctype="multipart/form-data">
    Name:<input type="text" name="name">
    File:<input type="file" name="attach" id="attach">
    <input type="submit">
</form>

表单中的enctype属性很重要。

有关详细信息,请参阅以下链接:http: //www.w3schools.com/php/php_file_upload.asp

还要确保 PHP 中的所有名称都与 HTML 匹配,例如文件输入的名称是attach,所以它应该在 PHP 中相同。

if($_FILES["attach"]["name"] != "")
{
    $strFilesName = $_FILES["attach"]["name"];
    ...
}
于 2013-01-19T07:32:50.037 回答