Guzzle 6大文件上传/分块

时间:2017-08-11 17:47:58

标签: php guzzle blueimp uploading

我已经读过,如果Guzzle无法确定Content-Length,它会发送Transfer-Encoding:Chunked标头和后端的cURL将处理分块。但我显然达到了post_max_size限制。 (" POST Content-Length为524288375字节超过了8388608字节的限制)当POST到工作的uploadChunkerController时。我知道上传处理程序(端点)适用于较小的文件。我觉得我的Guzzle选项配置错了。我必须将verify设置为false,我需要在请求中发布api_key

    $client = new Client();
    $fh     = fopen('../storage/random-500M.pdf', 'r');
    $url    = 'https://local:8443/app_dev.php/_uploader/bigupload/upload';

    $request = $client->request(
        'POST',
        $url,
        [
            'verify'    => false,
            'multipart' => [
                [
                    'name' => 'api_key',
                    'contents' => 'abc123'
                ],
                [
                    'name'     => 'file',
                    'contents' => $fh,
                    'filename' => 'bigupload.pdf'
                ]
            ]
        ]
    );

编辑php.ini设置不是一个选项,也不是解决方案。我找到了很多“解决方案”'这似乎是Guzzle的旧版本。我是否对此过于考虑?有更简单的解决方案吗?

2 个答案:

答案 0 :(得分:1)

在深入了解Guzzle和cURL源代码之后,没有自动'他们发送'块的方式。标头没有设置,他们无法切片正在发送的文件。我使用Guzzle vs原始PHP cURL调用提出了我自己的解决方案。

/**
 * @Route("/chunks", name="chunks")
 */
public function sendFileAction()
{
    $jar       = new \GuzzleHttp\Cookie\SessionCookieJar('SESSION_STORAGE', true);
    $handler   = new CurlHandler();
    $stack     = HandlerStack::create($handler);
    $client    = new Client(['cookies'=>true, 'handler' => $stack]);
    $filename  = 'files/huge-1gig-file.jpg';
    $filesize  = filesize($filename);
    $fh        = fopen($filename, 'r');
    $chunkSize = 1024 * 2000;
    $boundary  = '----iCEBrkUploaderBoundary' . uniqid();
    $url       = 'https://localhost/app_dev.php/_uploader/bigupload/upload';

    rewind($fh); // probably not necessary
    while (! feof($fh)) {
        $pos   = ftell($fh);
        $chunk = fread($fh, $chunkSize);
        $calc  = $pos + strlen($chunk)-1;

        // Not sure if this is needed.
        //if (ftell($fh) > $chunkSize) {
        //    $pos++;
        //}

        $request = $client->request(
            'POST',
            $url,
            [
                'cookies' => $jar,
                'debug'   => false,
                'verify'  => false,
                'headers' => [
                    'Transfer-Encoding'   => 'chunked',
                    'Accept-Encoding'     => 'gzip, deflate, br',
                    'Accept'              => 'application/json, text/javascript, */*; q=0.01',
                    'Connection'          => 'keep-alive',
                    'Content-disposition' => 'attachment; filename="' . basename($filename) . '"',
                    'Content-length'      => $calc - $pos,
                    'Content-Range'       => 'bytes ' . $pos . '-' . $calc . '/' . $filesize
                ],
                'multipart' => [
                    [
                        'name'     => 'api_key,
                        'contents' => 'aaabbbcc-deff-ffed-dddd-1234567890123'
                    ],
                    [
                        'name'     => 'file',
                        'contents' => $chunk,
                        'filename' => basename($filename),
                        'headers' => [
                            'Content-Type' => 'multipart/form-data; boundary=' . $boundary
                        ]
                    ]
                ]
            ]
        );
    }

    return new Response('ok', 200);
}

我希望这有助于其他人。欢迎提出意见/建议。

答案 1 :(得分:0)

在这种情况下,分块传输编码没有帮助。

它用于通过按部件发送(并生成)来提供内容。它与大小限制无关(如在您的方案中)。

唯一的方法是增加服务器的限制。