使用Guzzle PHP将文件大块上传到URL端点 [英] Upload File in chunks to URL Endpoint using Guzzle PHP

查看:671
本文介绍了使用Guzzle PHP将文件大块上传到URL端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用枪口将文件大块地上传到URL端点.

I want to upload files in chunks to a URL endpoint using guzzle.

我应该能够提供Content-Range和Content-Length标头.

I should be able to provide the Content-Range and Content-Length headers.

使用php,我知道我可以使用

Using php I know I can split using

define('CHUNK_SIZE', 1024*1024); // Size (in bytes) of chunk

function readfile_chunked($filename, $retbytes = TRUE) {
    $buffer = '';
    $cnt    = 0;
    $handle = fopen($filename, 'rb');

    if ($handle === false) {
        return false;
    }

    while (!feof($handle)) {
        $buffer = fread($handle, CHUNK_SIZE);
        echo $buffer;
        ob_flush();
        flush();

        if ($retbytes) {
            $cnt += strlen($buffer);
        }
    }

    $status = fclose($handle);

    if ($retbytes && $status) {
        return $cnt; // return num. bytes delivered like readfile() does.
    }

    return $status;
}

如何使用guzzle(如果可能的话,使用guzzle流)以块的形式发送文件?

How Do I achieve sending the files in chunk using guzzle, if possible using guzzle streams?

推荐答案

此方法允许您使用大量流传输大文件:

This method allows you to transfer large files using guzzle streams:

use GuzzleHttp\Psr7;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;

$resource = fopen($pathname, 'r');
$stream = Psr7\stream_for($resource);

$client = new Client();
$request = new Request(
        'POST',
        $api,
        [],
        new Psr7\MultipartStream(
            [
                [
                    'name' => 'bigfile',
                    'contents' => $stream,
                ],
            ]
        )
);
$response = $client->send($request);

这篇关于使用Guzzle PHP将文件大块上传到URL端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆