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

发布于 2024-09-10 18:51:12 字数 1301 浏览 6 评论 0 原文

我昨天问了这个问题并得到了建议,并使用了它,但由于某种原因它不起作用。 因此,我需要检索用户从 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/classes/Swift/ByteStream/ 中未捕获异常“Swift_IoException”,并显示消息“无法打开文件进行读取 [/tmp/phpHJdw0H]” FileByteStream.php:133 堆栈跟踪: #0
等等

谢谢!

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. I need this file to be attached to an email that is to be sent by 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
etc.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2024-09-17 18:51:12

使用:

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

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

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. This assumes the $target_path is within scope to the swift mailer code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文