`
FZtree
  • 浏览: 109666 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

PHP-handle-multipart-form-data-via-put-request

阅读更多

很明显,php不支持$_PUT

 

PUT方法提交的简单form可以

parse_str(file_get_contents('php://input'), $data);

 

Content-Type:multipart/form-data; 这样的form通过put方法提交就很尴尬了

解决这个问题得有思路

 

先去找找开源代码

https://stackoverflow.com/questions/9464935/

 

实际使用发现有bug,贴一下修改后的

 

// 解析 http 请求类型是 multipart/form-data 的数据
function parse_multipart_form(){
    // Fetch content and determine boundary
    $raw_data = file_get_contents('php://input');
    $boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));

    // Fetch each part
    $parts = array_slice(explode($boundary, $raw_data), 1);
    $data = array();

    foreach ($parts as $part) {
        // If this is the last part, break
        if ($part == "--\r\n") break;
        // Separate content from headers
        $part = ltrim($part, "\r\n");
        list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
        // Parse the headers list
        $raw_headers = explode("\r\n", $raw_headers);
        $headers = array();
        foreach ($raw_headers as $str) {
            $pos = strpos($str, ':');
            $name  = substr($str, 0, $pos) and $value = substr($str, 1+$pos);
            $headers[strtolower($name)] = ltrim($value, ' ');
        }
        // Parse the Content-Disposition to get the field name, etc.
        if (isset($headers['content-disposition'])) {
            preg_match(
                '/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
                $headers['content-disposition'],
                $matches
            );
            list(,,$name) = $matches;
            $data[$name] = substr($body, 0, strlen($body) - 2);
            isset($matches[4]) and $data[$name] = ['filename'=>$matches[4],'binary'=>$data[$name]];
        }
    }
    return $data;
}

 

 

 

就这样了

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics