phpMailer发送附件名称,而不是附件

时间:2011-07-14 19:52:44

标签: php phpmailer email-attachments

您好我有一个名为=“file1”的文件上传字段和phpmailer脚本中的代码:

if (isset($_FILES['file1']))
{
$file1_path = $_FILES['file1']['tmp_name'];
$file1_name = $_FILES['file1']['name'];
$file1_type = $_FILES['file1']['type'];
$file1_error = $_FILES['file1']['error'];
$mail->AddAttachment($file1_path);
}

由于某种原因,它附加了像php45we34(每次diff,似乎它的临时名称路径,而不是实际文件)

任何帮助?

3 个答案:

答案 0 :(得分:2)

从文件名中删除空格!

Blue hills.jpg应为Blue_hills.jpg

$fileName = str_replace(' ', '_', $fileName);

答案 1 :(得分:1)

我建议你在添加附件之前使用函数move_uploaded_file。

这是将文件从服务器某处的临时位置移动的示例代码

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

之后,AddAttachment应该可以正常工作。

$mail->AddAttachment(basename($target_path . $_FILES['uploadedfile']['name']));

答案 2 :(得分:1)

你看到的是应该发生什么。 You do not specify the attachment name,所以phpMailer使用它附加的临时文件的名称。

如果您希望文件具有不同的名称,则必须指定该名称。接受的答案是有效的,因为它反过来 - 它更改文件名,以便文件具有所需的名称。

通常的方法是发出

$mail->AddAttachment($file1_path, $_FILES['file1']['name']);

覆盖附件名称。