你打电话时
move_uploaded_file($file_tmp,"uploads/".$file_name);
这会在以下位置创建一个文件uploads/
目录,其文件名与上传者计算机上的名称相同。
然后,您使用示例代码将附件添加到 phpMailer,因此您基本上是在尝试附加不存在的文件。
这两行:
$mail->addAttachment('uploads/file.tar.gz'); // I took this from the phpmailer example on github but I'm not sure if I have it right.
$mail->addAttachment('uploads/image.jpg', 'new.jpg');
应改为:
$mail->addAttachment("uploads/".$file_name);
另请注意,无需调用move_uploaded_file
如果您不想在上传并通过电子邮件发送附件后保存附件。如果是这种情况,请致电AddAttachment
with $_FILES['image']['tmp_name']
作为文件参数。
另外,在您的 HTML 表单中,您有
<input id="file" name="file" type="file" />
但将输入称为image
在代码中。您应该更改该输入的名称file
to image
.
要仅附加图像而不保存它,请取出move_uploaded_file
代码并添加:
$file_tmp = $_FILES['image']['tmp_name'];
$file_name = $_FILES['image']['name'];
//...
$mail->AddAttachment($file_tmp, $file_name);