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);
    }

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.