解决php PUT PATCH 上传文件收不到数据

class FormDataParser
{

    private static $partSize = 4096;    //每次最大获取字节

    /**
     * 负责解析FormData
     */
    public static function parser($options = [])
    {
        //$options['saveFile'] = true; 测试能否正常保存临时文件

        $formData = fopen("php://input", "r");

        $retData = [];

        $boundary = rtrim(fgets($formData), "\r\n");     //第一行是boundary

        $info = []; //info段的信息
        $data = ''; //拼接的数据
        $infoPart = true; //是否是info段

        while ($line = fgets($formData, self::$partSize)) {
            if ($boundary . "\r\n" == $line || $boundary . "--\r\n" == $line) {
                //如果是分割
                $infoPart = true;

                if ($info['type'] == 'json') {
                    $data = json_decode($data, true);
                    $retData[$info['name']] = $data;
                } else if($info['type'] == 'file') {

                    if(isset($info['tmp_file'])) {
                        fclose($info['file_handle']);
                        $retData[$info['name']] = [
                            'org_name' => $info['org_name'],
                            'tmp_file' => $info['tmp_file']
                        ];
                    } else {
                        $retData[$info['name']] = $data;
                    }

                }

                $data = '';
            } else if ("\r\n" == $line) {
                if ($infoPart) {
                    //解析info
                    $info = self::parserInfo($data, $options);
                    if (isset($info['tmp_file'])) {
                        $info['file_handle'] = fopen($info['tmp_file'], 'w');
                    }
                    $data = '';
                    $infoPart = false;
                }
            } else {
                if($infoPart == false && isset($info['tmp_file'])) {
                    fwrite($info['file_handle'], $line);
                } else {
                    $data .= $line;
                }
            }
        }
        fclose($formData);

        print_r($retData);
    }

    private static function parserInfo($data, $options)
    {
        //获取参数名称, type
        $infoPattern = '/name="(.+?)"(; )?(filename="(.+?)")?/'; //todo: 待优化
        preg_match($infoPattern, $data, $matches);

        $info['name'] = $matches[1];
        $info['type'] = 'json';

        //如果是文件
        if (count($matches) > 4) {
            $info['type'] = 'file';
            $info['org_name'] = $matches[4];
            //如果设置保存文件, 保存到临时文件
            if (isset($options['saveFile']) && $options['saveFile']) {
                $tmpFile = tempnam(sys_get_temp_dir(), 'FD');
                $info['tmp_file'] = $tmpFile;
            }
        }

        return $info;
    }

}



put patch 用这个方法可以接受文件  和数据 ,解决表单乱码。  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值