【问题标题】:Email attachments not working in PHP电子邮件附件在 PHP 中不起作用
【发布时间】:2014-08-23 07:40:31
【问题描述】:

我试图在我的网站上添加一项功能,允许用户在发送电子邮件时添加附件。我用 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>

【问题讨论】:

  • 让你给 0777 目录权限
  • 我没有.. 怎么办??它需要一些其他的PHP代码吗??
  • 你可以通过 ftp 客户端来实现...而且,我肯定会使用一些 php 库(例如 phpmailer)来发送带有附件的电子邮件 -> 没有它会很痛苦...
  • 你用的是linux还是windows
  • 我使用的是 windows 8 64 位..

标签: php email attachment


【解决方案1】:
<?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>

【讨论】:

  • 工作老兄简单粘贴代码并检查文件是否在文件夹中上传
  • 另外,我收到此错误:Array ( [attachment] => Array ( [name] => Untitled.jpg [type] => image/jpeg [tmp_name] => C:\Windows \Temp\phpF9EB.tmp [错误] => 0 [大小] => 108912 ) )
  • 现在所有代码都运行良好。谢谢。删除 print_r($_FILES); 后,该错误也消失了。请问你为什么需要那条线??
  • kk 非常感谢.. 抱歉,由于我今天才加入 Stackoverflow,因此我无法为您的答案投票。
  • 勾选它作为答案,你可以这样做
猜你喜欢
  • 2015-12-29
  • 2015-06-06
  • 1970-01-01
  • 2016-07-15
  • 2014-05-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
  • 2013-01-02
相关资源
最近更新 更多