2010-07-20 51 views
4

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

//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']); 

为什么它不从服务器获取该文件? 以下是错误消息,它看起来像它试图找到文件中的一个错误的目录:

Warning: fopen(/tmp/phpHJdw0H) [function.fopen]: failed to open stream: No such file or directory in /home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php on line 131 Fatal error: Uncaught exception 'Swift_IoException' with message 'Unable to open file for reading [/tmp/phpHJdw0H]' in /home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib/classes/Swift/ByteStream/FileByteStream.php:133 Stack trace: #0
etc.

谢谢!

回答

4

用途:

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

这是因为你已经从它的临时位置,$_FILES['uploadedfile']['tmp_name']从您移动使用move_uploaded_file文件之前返回路径的文件。这假设$target_path是在范围内迅速邮寄代码

+0

它的工作! 谢谢,谢谢!万岁! :) – vlevsha 2010-07-20 18:02:59

相关问题