我昨天问了这个问题并得到了建议,并使用了它,但由于某种原因它不起作用。
因此,我需要检索用户从 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!
发布评论
评论(1)
使用:
这是因为您已将文件从其临时位置移动,
$_FILES['uploadedfile']['tmp_name']
返回使用 move_uploaded_file。这假设$target_path
在 swift 邮件程序代码的范围内Use:
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