PHP将非常大的多部分文件发送到另一个Web服务器

时间:2016-05-20 18:46:26

标签: php curl php-stream-wrappers

我已经使用通过多形式帖子将文件发送到另一个网站的流编写了一些代码。所以这是php服务器代码 - >另一个Web服务器。我目前无法使用“卷曲”,我只限于流。

我的担忧(下面的评论,关注点),如果我遇到一个非常大的文件,它可能会耗尽php web服务器上的内存。

当使用指向url(rest api端点)的php流时,有没有办法使用PHP流直接写入请求流?现在一切都放在一起之后,就会发出一个

的file_get_contents(...)

所以这里有一段代码,用于创建和发送文件。

    private function getOptionsForMulitPartFile($request, $cred, $file)
    {

        $mimeType = $this->getMimeType($file);
        $contentType = 'Content-Type: multipart/form-data; boundary=' . MULTIPART_BOUNDARY;
        $contentLength = "Content-Length: " . filesize($file->getFilePathAndName());

        $file_contents = $file->readWholeFile(); //<----Concern here, that file maybe too large.

        $data =  "--" . MULTIPART_BOUNDARY . "\r\n" .
            "Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"" . basename($file->getFilePathAndName()) . "\"\r\n" .
            "Content-Type: " . $mimeType . "\r\n\r\n" . $file_contents."\r\n";

        $data  .=  "--". MULTIPART_BOUNDARY . "--\r\n";

        // set up the request context

        return $this->createOption($request, $cred, $data, $contentType, $contentLength);
    }

1 个答案:

答案 0 :(得分:0)

在读取大文件时,可以使用ob_flush() + stream_copy_to_stream方法,以便从文件读取的每个X字节都可以传输到另一台服务器。