加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:03:57 所属栏目:PHP教程 来源:网络整理
导读:我昨天问了这个问题并得到了一个建议并使用了它,但由于某种原因它没有用. 因此,我需要检索用户从 HTML表单上传到我的服务器的文件的名称.我需要将此文件附加到由 PHP / SwiftMailer发送的电子邮件中. 这是我的代码,文件上传部分: //File upload// Where the
我昨天问了这个问题并得到了一个建议并使用了它,但由于某种原因它没有用.
因此,我需要检索用户从 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']);

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

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.

谢谢!

解决方法

使用:

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读