4

我昨天问了这个问题并得到了一个建议,并使用了它,但由于某种原因它不起作用。因此,我需要从用户的 HTML 表单中检索上传到我的服务器的文件的名称。我需要将此文件附加到将由 PHP/SwiftMailer 发送的电子邮件中。这是我的代码,文件上传部分:

    //File upload

// Where the file is going to be placed 
$target_path = "uploads/";

// Add the original filename to our target path.  
//Result is "uploads/filename.extension" 
$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!";
}   

//End of file upload

这是文件附件部分:

//Create the attachment
$attachment = Swift_Attachment::fromPath($_FILES['uploadedfile']['tmp_name']);

为什么它不从服务器获取文件?这是错误消息,看起来它试图在错误的目录中查找文件:

警告:fopen(/tmp/phpHJdw0H)[function.fopen]:无法打开流:/home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib/classes/Swift/ByteStream 中没有这样的文件或目录/FileByteStream.php 第 131 行致命错误:在 /home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib 中出现消息“无法打开文件以读取 [/tmp/phpHJdw0H]”的未捕获异常“Swift_IoException” /classes/Swift/ByteStream/FileByteStream.php:133 堆栈跟踪:#0
等。

谢谢!

4

1 回答 1

5

利用:

$attachment = Swift_Attachment::fromPath($target_path);

这是因为您已将文件从其临时位置移动,返回使用move_uploaded_file$_FILES['uploadedfile']['tmp_name']移动文件之前的路径。这假设在swift mailer 代码的范围内$target_path

于 2010-07-20T17:39:06.750 回答