微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Mailgun表单PHP中检索附件

如何检索并保存通过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.

原文地址:https://www.jb51.cc/html/226077.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐