如何禁用PHP邮件程序中的“无法访问文件”消息?

时间:2022-03-18 18:12:45

I am using PHPMailer for sending mail from form with multiple attachments. This is a part of the form:

我正在使用PHPMailer从具有多个附件的表单发送邮件。这是表单的一部分:

 Attachment:<br />
<input type="file" name="attach1" id="attach1" />
<input type="file" name="attach2" id="attach2" />
<input type="file" name="attach3" id="attach3" />

When a user attaches only 1 or 2 files, I added some condition in my email.php:

当用户只附加1或2个文件时,我在email.php中添加了一些条件:

if(!empty($_FILES['attach1'] ))
{
$mail->AddAttachment($_FILES['attach1']['tmp_name']); // bijlage 1
}
if(!empty($_FILES['attach2']))
{
$mail->AddAttachment($_FILES['attach2']['tmp_name']); // bijlage 2
}
if(!empty($_FILES['attach3']))
{
$mail->AddAttachment($_FILES['attach3']['tmp_name']); // bijlage 3
}

But I still got the message "could not access file" on the empty input fields. The attachments are being sent correctly to the email address even with this message. But I want to get rid of it when I use only 1 or 2 attachments.

但我仍然在空输入字段上收到“无法访问文件”的消息。即使有此消息,附件也会正确发送到电子邮件地址。但是当我只使用1或2个附件时,我想摆脱它。

2 个解决方案

#1


All right. This line :

行。这一行:

if(!empty($_FILES['attach3']))..

isn't equal false ( array in $_FILES['attach3'] isn't empty: it has ,e.g, error code eq 4 (UPLOAD_ERR_NO_FILE) in $_FILES['attach3']['error'] and some another fields) when you send only 1 and 2 attachements => you see message with "could not access file" in third case. You need to change checks to:

不等于false($ _FILES ['attach3']中的数组不为空:它在$ _FILES ['attach3'] ['error']和其他一些字段中有例如错误代码eq 4(UPLOAD_ERR_NO_FILE)当你只发送1和2个附件=>时,你会看到第三种情况下“无法访问文件”的消息。您需要将支票更改为:

if(!empty($_FILES['attach1']['name'] ))....

if(!empty($_FILES['attach2']['name'] ))....

if(!empty($_FILES['attach3']['name'] ))....

#2


You're doing this wrong - you should use move_uploaded_file before accessing the files in $_FILES, as per the docs and the example code provided with PHPMailer.

你做错了 - 你应该在访问$ _FILES中的文件之前使用move_uploaded_file,根据文档和PHPMailer提供的示例代码。

#1


All right. This line :

行。这一行:

if(!empty($_FILES['attach3']))..

isn't equal false ( array in $_FILES['attach3'] isn't empty: it has ,e.g, error code eq 4 (UPLOAD_ERR_NO_FILE) in $_FILES['attach3']['error'] and some another fields) when you send only 1 and 2 attachements => you see message with "could not access file" in third case. You need to change checks to:

不等于false($ _FILES ['attach3']中的数组不为空:它在$ _FILES ['attach3'] ['error']和其他一些字段中有例如错误代码eq 4(UPLOAD_ERR_NO_FILE)当你只发送1和2个附件=>时,你会看到第三种情况下“无法访问文件”的消息。您需要将支票更改为:

if(!empty($_FILES['attach1']['name'] ))....

if(!empty($_FILES['attach2']['name'] ))....

if(!empty($_FILES['attach3']['name'] ))....

#2


You're doing this wrong - you should use move_uploaded_file before accessing the files in $_FILES, as per the docs and the example code provided with PHPMailer.

你做错了 - 你应该在访问$ _FILES中的文件之前使用move_uploaded_file,根据文档和PHPMailer提供的示例代码。