简体   繁体   English

电子邮件附件在PHP中不起作用

[英]Email attachments not working in PHP

I was trying to add a feature on my website which allows users to add attachments while sending emails. 我试图在我的网站上添加一项功能,该功能允许用户在发送电子邮件时添加附件。 I tried this with PHP, but the code is not working. 我用PHP尝试过,但是代码不起作用。 It is neither uploading the attachment, nor inserting the URL of the attachment in my database, nor echoing the confirmation message. 它既没有上传附件,也没有在数据库中插入附件的URL,也没有回显确认消息。

All the other form code is processed successfully, except the attachment. 除附件外,所有其他表单代码均已成功处理。 Please suggest me what changes should be made to the code: 请建议我对代码进行哪些更改:

PHP: 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;
}

Later in the script, $url is to be inserted in the MySQL database, but that doesn't work either. 在脚本的后面,将$url插入MySQL数据库,但这也不起作用。 Just a blank variable is inserted in the database. 在数据库中仅插入一个空白变量。

Here's the HTML code: 这是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>
<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM