欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-21 10:35:56
...

很明显,php不支持$_PUT

 

PUT方法提交的简单form可以

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

 

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

解决这个问题得有思路

 

先去找找开源代码

https://*.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;
}

 

 

 

就这样了