userland multipart/form-data处理程序

mar*_*rio 9 php multipartform-data

我正在寻找一个包含脚本/类的插件multipart/form-data,它可以解析并填充$_POST(+ raw)并$_FILES从中填充.通常PHP会自己做.但是因为自动处理对我来说是不够的并且使得php://input不可能[1]我可能会使用这样的东西来防止:

RewriteRule .* - [E=CONTENT_TYPE:noparsing/for-you-php]
不行.实际解决方案需要mod_headers和RequestHeader set......

提取过程可能不那么复杂.但我宁愿使用经过充分测试的解决方案.最重要的是,我更喜欢fgets用于拆分的实现,并且$_FILES密切有效地模仿处理.找到二进制有效负载的结束对我来说似乎相当棘手,特别是当您必须剥离\r\n但可能遇到仅发送\n(不允许但可能)的客户端时.

我确定这样的事情存在.但是我很难用谷歌搜索它.有谁知道实施?(PEAR :: mimeDecode可以被黑客攻击以获得表单数据的工作方式,但是内存耗尽.)

用例简而言之:需要保留原始字段名称(包括空格和特殊字符),以便进行日志记录,但始终无法避免文件上载.


出于装饰目的,这就是POST请求的外观:

POST / HTTP/1.1
Host: localhost:8000
Content-Length: 17717
Content-Type: multipart/form-data; boundary=----------3wCuBwquE9P7A4OEylndVx
Run Code Online (Sandbox Code Playgroud)

在一个\r\n\r\n序列之后,multipart/payload遵循如下:

------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name="_charset_"

windows-1252
------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name=" text field \\ 1 \";inject=1"

text1 te twj sakfkl
------------3wCuBwquE9P7A4OEylndVx
Content-Disposition: form-data; name="file"; filename="dial.png"
Content-Type: image/png

IPNG Z @@@MIHDR@@B`@@B;HF@@@-'.e@@@AsRGB@.N\i@@@FbKGD@?@?@? ='S@@@     
@@@GtIMEGYAAU,#}BRU@@@YtEXtComment@Created with GIMPWANW@@ @IDATxZl]w|
Run Code Online (Sandbox Code Playgroud)

Ali*_*xel 4

已经晚了,我现在无法对此进行测试,但以下内容应该可以满足您的要求:

//$boundary = null;

if (is_resource($input = fopen('php://input', 'rb')) === true)
{

    while ((feof($input) !== true) && (($line = fgets($input)) !== false))
    {
        if (isset($boundary) === true)
        {
            $content = null;

            while ((feof($input) !== true) && (($line = fgets($input)) !== false))
            {
                $line = trim($line);

                if (strlen($line) > 0)
                {
                    $content .= $line . ' ';
                }

                else if (empty($line) === true)
                {
                    if (stripos($content, 'name=') !== false)
                    {
                        $name = trim(stripcslashes(preg_replace('~.*name="?(.+)"?.*~i', '$1', $content)));

                        if (stripos($content, 'Content-Type:') !== false)
                        {
                            $tmpname = tempnam(sys_get_temp_dir(), '');

                            if (is_resource($temp = fopen($tmpname, 'wb')) === true)
                            {
                                while ((feof($input) !== true) && (($line = fgets($input)) !== false) && (strpos($line, $boundary) !== 0))
                                {
                                    fwrite($temp, preg_replace('~(?:\r\n|\n)$~', '', $line));
                                }

                                fclose($temp);
                            }

                            $FILES[$name] = array
                            (
                                'name' => trim(stripcslashes(preg_replace('~.*filename="?(.+)"?.*~i', '$1', $content))),
                                'type' => trim(preg_replace('~.*Content-Type: ([^\s]*).*~i', '$1', $content)),
                                'size' => sprintf('%u', filesize($tmpname)),
                                'tmp_name' => $tmpname,
                                'error' => UPLOAD_ERR_OK,
                            );
                        }

                        else
                        {
                            $result = null;

                            while ((feof($input) !== true) && (($line = fgets($input)) !== false) && (strpos($line, $boundary) !== 0))
                            {
                                $result .= preg_replace('~(?:\r\n|\n)$~', '', $line);
                            }

                            if (array_key_exists($name, $POST) === true)
                            {
                                if (is_array($POST[$name]) === true)
                                {
                                    $POST[$name][] = $result;
                                }

                                else
                                {
                                    $POST[$name] = array($POST[$name], $result);
                                }
                            }

                            else
                            {
                                $POST[$name] = $result;
                            }
                        }
                    }

                    if (strpos($line, $boundary) === 0)
                    {
                        //break;
                    }
                }
            }
        }

        else if ((is_null($boundary) === true) && (strpos($line, 'boundary=') !== false))
        {
            $boundary = "--" . trim(preg_replace('~.*boundary="?(.+)"?.*~i', '$1', $line));
        }
    }

    fclose($input);
}

echo '<pre>';
print_r($POST);
echo '</pre>';

echo '<hr />';

echo '<pre>';
print_r($FILES);
echo '</pre>';
Run Code Online (Sandbox Code Playgroud)