简体   繁体   English

使用SwiftMailer和PHP检索要附加到电子邮件的文件名

[英]Retrieving a file name to attach to an email with SwiftMailer and PHP

I asked the question yesterday and got an advice, and used it, but it doesn't work for some reason. 我昨天问了这个问题并得到了一个建议并使用了它,但由于某种原因它没有用。 So, I need to retrieve the name of the file that was uploaded to my server from an HTML form by a user. 因此,我需要检索用户从HTML表单上传到我的服务器的文件的名称。 I need this file to be attached to an email that is to be sent by PHP/SwiftMailer. 我需要将此文件附加到由PHP / SwiftMailer发送的电子邮件中。 Here is my code, the file upload portion: 这是我的代码,文件上传部分:

    //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

This is the file attaching portion: 这是文件附加部分:

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

Why doesn't it get the file from the server? 为什么不从服务器获取文件? Here is the error message, and it looks like it's trying to find the file in a wrong directory: 这是错误消息,看起来它正在尝试在错误的目录中找到该文件:

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 警告:fopen(/ tmp / phpHJdw0H)[function.fopen]:无法打开流:/home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib/classes/Swift/ByteStream中没有这样的文件或目录第131行上的/FileByteStream.php致命错误:在/home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib中显示消息'无法打开文件以便阅读[/ tmp / phpHJdw0H]'的未捕获异常'Swift_IoException' /classes/Swift/ByteStream/FileByteStream.php:133堆栈跟踪:#0
etc. 等等

Thank you! 谢谢!

Use: 使用:

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

This is because you have moved the file from its temporary location, $_FILES['uploadedfile']['tmp_name'] returns the path from before you moved the file using move_uploaded_file . 这是因为您已将文件从其临时位置移动, $_FILES['uploadedfile']['tmp_name']返回使用move_uploaded_file移动文件之前的路径。 This assumes the $target_path is within scope to the swift mailer code 这假设$target_path在swift邮件程序代码的范围内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM