yii\httpclient\Request::prepareMultiPartContent PHP Method

prepareMultiPartContent() private method

Prepares multi-part content.
private prepareMultiPartContent ( array $content )
$content array multi part content.
    private function prepareMultiPartContent(array $content)
    {
        static $disallowedChars = ["", '"', "\r", "\n"];
        $contentParts = [];
        $data = $this->getData();
        if (!empty($data)) {
            foreach ($this->composeFormInputs($data) as $name => $value) {
                $name = str_replace($disallowedChars, '_', $name);
                $contentDisposition = 'Content-Disposition: form-data; name="' . $name . '";';
                $contentParts[] = implode("\r\n", [$contentDisposition, '', $value]);
            }
        }
        // process content parts :
        foreach ($content as $name => $contentParams) {
            $headers = [];
            $name = str_replace($disallowedChars, '_', $name);
            $contentDisposition = 'Content-Disposition: form-data; name="' . $name . '";';
            if (isset($contentParams['fileName'])) {
                $fileName = str_replace($disallowedChars, '_', $contentParams['fileName']);
                $contentDisposition .= ' filename="' . $fileName . '"';
            }
            $headers[] = $contentDisposition;
            if (isset($contentParams['contentType'])) {
                $headers[] = 'Content-Type: ' . $contentParams['contentType'];
            } elseif (isset($contentParams['mimeType'])) {
                $headers[] = 'Content-Type: ' . $contentParams['mimeType'];
            }
            $contentParts[] = implode("\r\n", [implode("\r\n", $headers), '', $contentParams['content']]);
        }
        // generate safe boundary :
        do {
            $boundary = '---------------------' . md5(mt_rand() . microtime());
        } while (preg_grep("/{$boundary}/", $contentParts));
        // add boundary for each part :
        array_walk($contentParts, function (&$part) use($boundary) {
            $part = "--{$boundary}\r\n{$part}";
        });
        // add final boundary :
        $contentParts[] = "--{$boundary}--";
        $contentParts[] = '';
        $this->getHeaders()->set('content-type', "multipart/form-data; boundary={$boundary}");
        $this->setContent(implode("\r\n", $contentParts));
    }