繁体   English   中英

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

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

我昨天问了这个问题并得到了一个建议并使用了它,但由于某种原因它没有用。 因此,我需要检索用户从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中没有这样的文件或目录第131行上的/FileByteStream.php致命错误:在/home/myserver/mydomain.com/Hawaii/html/swift-mailer/lib中显示消息'无法打开文件以便阅读[/ tmp / phpHJdw0H]'的未捕获异常'Swift_IoException' /classes/Swift/ByteStream/FileByteStream.php:133堆栈跟踪:#0
等等

谢谢!

使用:

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

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

暂无
暂无

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

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