duanlangwen9597 2016-05-20 18:46
浏览 128

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

I have written some code using streams that sends a file via a multiform post to another website. So this is php server code to -> another web server. I currently cannot use "curl", I constrained to streams.

My concern (Comment below, Concern here,) is if I encounter a very large file it may exhaust the memory on the php web server.

Is there a way of working with PHP streams to write directly to request stream when using php stream that point to a url (rest api endpoint)? right now after everything is put together, it issues a

file_get_contents(...)

So here is a snippet of code that is creating and sending a the file.

    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 . "
" .
            "Content-Disposition: form-data; name=\"".FORM_FIELD."\"; filename=\"" . basename($file->getFilePathAndName()) . "\"
" .
            "Content-Type: " . $mimeType . "

" . $file_contents."
";

        $data  .=  "--". MULTIPART_BOUNDARY . "--
";

        // set up the request context

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

1条回答 默认 最新

  • dongrong6235 2018-06-28 14:58
    关注

    While reading large files you can use ob_flush() + stream_copy_to_stream methods so every X byte you read from file you can transfer to another server.

    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路