【问题标题】:PHPMailer File Upload Not WorkingPHPMailer文件上传不起作用
【发布时间】:2017-12-19 21:43:14
【问题描述】:

我的联系表格运行良好,但我无法通过联系表格获取上传的文件。此处为 HTML 代码;

<form id="contact_form" method="post" action="php/mail-advanced.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" multiple />
<label for="file"></label>
<button type="submit" id="submit">SEND MESSAGE</button></form>

这些代码在我的 php/mail-advanced.php 文件中;

<?php

date_default_timezone_set('Etc/UTC');
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = "smtp.example.com";
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = "noreply@example.com";
$mail->Password = "pass";
$mail->setFrom('noreply@example.com', 'Example Message');
$mail->addReplyTo($email);
$mail->addAddress('example@gmail.com', 'Example Message');
$mail->Subject = $subject;
$mail->addAttachment($_FILES['file']);

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

如何在 PHPMailer 上通过文件上传获取文件?谢谢!

【问题讨论】:

标签: php forms file upload phpmailer


【解决方案1】:

您需要在 PHP 中传递文件的临时名称。和实际上传的方式基本一样。

$mail->AddAttachment($_FILES['file']['tmp_name'], $_FILES['file']['name']);

【讨论】:

  • 这是不安全的。阅读the docs
  • 我没说安全,只是直接回答了他的问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-27
  • 2014-01-09
  • 2013-03-17
  • 2013-12-09
  • 2015-02-11
  • 2011-10-10
相关资源
最近更新 更多