昨天我问了这个问题并得到了建议,并使用了它,但是由于某些原因它没有用。因此,我需要检索用户从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
Run Code Online (Sandbox Code Playgroud)
这是文件附加部分:
//Create the attachment
$attachment = Swift_Attachment::fromPath($_FILES['uploadedfile']['tmp_name']);
Run Code Online (Sandbox Code Playgroud)
为什么不从服务器获取文件?这是错误消息,它似乎正在尝试在错误的目录中查找文件:
警告: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中未捕获的异常'Swift_IoException',消息为'无法打开文件以读取[/ tmp / phpHJdw0H]' /classes/Swift/ByteStream/FileByteStream.php:133堆栈跟踪:#0
等
谢谢!
使用:
$attachment = Swift_Attachment::fromPath($target_path);
Run Code Online (Sandbox Code Playgroud)
这是因为您已从文件的临时位置$_FILES['uploadedfile']['tmp_name']移动了文件,并使用move_uploaded_file返回了文件移动之前的路径。假设处于$target_path快速邮件程序代码的范围内