如何检索并保存通过Mailgun POST的表单发送给我的附件
以下是一些参数
attachment-1 {:filename=>"crabby.gif",:type=>"image/gif",:name=>"attachment-1",:tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-slsrkh>,:head=>"Content-Disposition: form-data; name=\"attachment-1\"; filename=\"crabby.gif\"\r\nContent-Type: image/gif\r\nContent-Length: 2785\r\n"} attachment-2 {:filename=>"attached_файл.txt",:type=>"text/plain",:name=>"attachment-2",:tempfile=>#<Tempfile:/tmp/RackMultipart20140707-2-sudxuf>,:head=>"Content-Disposition: form-data; name=\"attachment-2\"; filename=\"attached_файл.txt\"\r\nContent-Type: text/plain\r\nContent-Length: 32\r\n"} Content-Type multipart/mixed; boundary="------------020601070403020003080006"
解决方法
所以我知道这是迟了一年,但我遇到了同样的问题,并想出了如何下载附件.帖子中的文件存储在环境变量
$_FILES中.每个文件的信息如下所示:
Array ( [attachment-1] => Array ( [name] => ATextFile.txt [type] => text/plain [tmp_name] => /tmp/PHP8zhmlU [error] => 0 [size] => 70144 ) )
文件的路径存储在tmp_name中,因此在这种情况下,/ tmp / PHP8zhmlU是文件的完整路径. move_uploaded_file将覆盖任何现有文件!要从POST下载所有附件我写了一个函数:
function download_attachments($pathToDownloadDirectory) { foreach($_FILES as $file) { if($file['error'] == "0") { if(!(move_uploaded_file($file['tmp_name'],$pathToDownloadDirectory . $file['name']))) { return 0; } } else { return 0; } } return 1; } download_attachments("/Full/Path/To/Some/Dir/");
此顶部的文档可以是found here.