电子邮件附件不适用于PHP

时间:2014-08-23 07:40:31

标签: php email attachment

我试图在我的网站上添加一项功能,允许用户在发送电子邮件时添加附件。我用PHP尝试了这个,但代码不起作用。它既不上传附件,也不在我的数据库中插入附件的URL,也不回显确认消息。

除附件外,所有其他表单代码都已成功处理。请建议我对代码进行哪些更改:

PHP:

if (isset($_POST['attachment']))
{
    if ($_FILES['file']['size'] > 52428800)
    {
        $attachment_results =  "Sorry, your attachment could not be processed as it is exceeding the limit of 50 MB.";
    }
    else
    {
        if ($_FILES["file"]["error"] > 0)
        {
           $attachment_results =  "Sorry, your attachment could not be processed due to some error. Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            $attachment_results = "Your attachment was also processed successfully.<br>
            File name: " . $_FILES['file']['name'] . "<br>Attachment Size: " . $_FILES['file']['size'];
            $destination = "attachments/" . $_FILES['file']['name'];
            move_uploaded_file($_FILES['file']['name'], $destination);
            $url = "https://$domain_name/$destination";
        }
    }
    echo $attachment_results;
}

稍后在脚本中,$url将插入MySQL数据库中,但这也不起作用。只需在数据库中插入一个空白变量。

这是HTML代码:

<form enctype="multipart/form-data" method="post" action="">
<input type="file" name="attachment" style="color: #000000;"><label>(Maximum 50 MB)    </label>
<!-- Other Form Code -->
</form>

1 个答案:

答案 0 :(得分:0)

<?php 
if (isset($_POST['submit']))
{

print_r($_FILES);
    if ($_FILES['attachment']['size'] > 52428800)
    {
        $attachment_results =  "Sorry, your attachment could not be processed as it is exceeding the limit of 50 MB.";
    }
    else
    {
        if ($_FILES["attachment"]["error"] > 0)
        {
           $attachment_results =  "Sorry, your attachment could not be processed due to some error. Return Code: " . $_FILES["file"]["error"] . "<br>";
        }
        else
        {
            $attachment_results = "Your attachment was also processed successfully.<br>
            File name: " . $_FILES['attachment']['name'] . "<br>Attachment Size: " . $_FILES['file']['size'];
            $destination = "attachments/" . $_FILES['attachment']['name'];
            move_uploaded_file($_FILES['attachment']['tmp_name'], $destination);
            $url = "https://$domain_name/$destination";
        }
    }
    echo $attachment_results;
}
?>


<form enctype="multipart/form-data" method="post" action="">
<input type="file" name="attachment" style="color: #000000;"><label>(Maximum 50 MB)    </label>
<input type="submit" name="submit">
</form>