douru5373 2019-01-18 10:56
浏览 116

PHP - 带附件提交的联系表单 - file_get_contents():文件名不能为空

I've been making a contact form that has an attachment option, but when I tried it, I got this message

"Warning: file_get_contents(): Filename cannot be empty in sample-name.php on line 26

Cannot send mail."

How can I fix this?

Here's my HTML code:

<body>

    <form action="php_sendmail_upload.php" method="post" name="form1" enctype="multipart/form-data">
        <table width="343" border="1">
            <tr>
            <td>To</td>
            <td><input name="txtTo" type="text" id="txtTo"></td>
            </tr>
            <tr>
            <td>Subject</td>
            <td><input name="txtSubject" type="text" id="txtSubject"></td>
            </tr>
            <tr>
            <td>Description</td>
            <td><textarea name="txtDescription" cols="30" rows="4" id="txtDescription"></textarea></td>
            </tr>
            <tr>
            <td>Form Name</td>
            <td><input name="txtFormName" type="text"></td>
            </tr>
            <tr>
            <tr>
            <td>Form Email</td>
            <td><input name="txtFormEmail" type="text"></td>
            </tr>
            <tr>
            <td>Attachment</td>
            <td><input name="fileAttach" type="file"></td>
            </tr>
            <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit" value="Send"></td>
            </tr>
        </table>
    </form>

</body>

Here's my PHP code (php_sendmail_upload.php):

<?php
// $strTo = $_POST["txtTo"];
$strTo = "philip.santiago@yr.com";
$strSubject = "Email attach subject";
$strMessage = nl2br($_POST["txtDescription"]);

//*** Uniqid Session ***//
$strSid = md5(uniqid(time()));

$strHeader = "";
$strHeader .= "From: ".$_POST["txtFormName"]."<".$_POST["txtFormEmail"].">
Reply-To: ".$_POST["txtFormEmail"]."";

$strHeader .= "MIME-Version: 1.0
";
$strHeader .= "Content-Type: multipart/mixed; boundary=\"".$strSid."\"

";
$strHeader .= "This is a multi-part message in MIME format.
";

$strHeader .= "--".$strSid."
";
$strHeader .= "Content-type: text/html; charset=utf-8
";
$strHeader .= "Content-Transfer-Encoding: 7bit

";
$strHeader .= $strMessage."

";

//*** Attachment ***//
if($_FILES["fileAttach"]["name"] != "")
{
$strFilesName = $_FILES["fileAttach"]["name"];
// THE LINE BELOW SEEMS TO BE CAUSING THE ERROR //
$strContent = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));

$strHeader .= "--".$strSid."
";
$strHeader .= "Content-Type: application/octet-stream; name=\"".$strFilesName."\"
";
$strHeader .= "Content-Transfer-Encoding: base64
";
$strHeader .= "Content-Disposition: attachment; filename=\"".$strFilesName."\"

";
$strHeader .= $strContent."

";
}

$flgSend = @mail($strTo,$strSubject,null,$strHeader);  // @ = No Show Error //

if($flgSend)
{
echo "Mail send completed.";
}
else
{
echo "Cannot send mail.";
}
?>

Thank you in advance! :)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
    • ¥15 C#调用python代码(python带有库)
    • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
    • ¥15 活动选择题。最多可以参加几个项目?
    • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
    • ¥15 vs2019中数据导出问题
    • ¥20 云服务Linux系统TCP-MSS值修改?
    • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
    • ¥20 怎么在stm32门禁成品上增加查询记录功能
    • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面