2 namespace GuzzleHttp\Psr7;
4 use Psr\Http\Message\StreamInterface;
7 * Stream that when read returns bytes for a streaming multipart or
8 * multipart/form-data stream.
10 class MultipartStream implements StreamInterface
12 use StreamDecoratorTrait;
17 * @param array $elements Array of associative arrays, each containing a
18 * required "name" key mapping to the form field,
19 * name, a required "contents" key mapping to a
20 * StreamInterface/resource/string, an optional
21 * "headers" associative array of custom headers,
22 * and an optional "filename" key mapping to a
23 * string to send as the filename in the part.
24 * @param string $boundary You can optionally provide a specific boundary
26 * @throws \InvalidArgumentException
28 public function __construct(array $elements = [], $boundary = null)
30 $this->boundary = $boundary ?: sha1(uniqid('', true));
31 $this->stream = $this->createStream($elements);
39 public function getBoundary()
41 return $this->boundary;
44 public function isWritable()
50 * Get the headers needed before transferring the content of a POST file
52 private function getHeaders(array $headers)
55 foreach ($headers as $key => $value) {
56 $str .= "{$key}: {$value}\r\n";
59 return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
63 * Create the aggregate stream that will be used to upload the POST data
65 protected function createStream(array $elements)
67 $stream = new AppendStream();
69 foreach ($elements as $element) {
70 $this->addElement($stream, $element);
73 // Add the trailing boundary with CRLF
74 $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
79 private function addElement(AppendStream $stream, array $element)
81 foreach (['contents', 'name'] as $key) {
82 if (!array_key_exists($key, $element)) {
83 throw new \InvalidArgumentException("A '{$key}' key is required");
87 $element['contents'] = stream_for($element['contents']);
89 if (empty($element['filename'])) {
90 $uri = $element['contents']->getMetadata('uri');
91 if (substr($uri, 0, 6) !== 'php://') {
92 $element['filename'] = $uri;
96 list($body, $headers) = $this->createElement(
99 isset($element['filename']) ? $element['filename'] : null,
100 isset($element['headers']) ? $element['headers'] : []
103 $stream->addStream(stream_for($this->getHeaders($headers)));
104 $stream->addStream($body);
105 $stream->addStream(stream_for("\r\n"));
111 private function createElement($name, StreamInterface $stream, $filename, array $headers)
113 // Set a default content-disposition header if one was no provided
114 $disposition = $this->getHeader($headers, 'content-disposition');
116 $headers['Content-Disposition'] = ($filename === '0' || $filename)
117 ? sprintf('form-data; name="%s"; filename="%s"',
120 : "form-data; name=\"{$name}\"";
123 // Set a default content-length header if one was no provided
124 $length = $this->getHeader($headers, 'content-length');
126 if ($length = $stream->getSize()) {
127 $headers['Content-Length'] = (string) $length;
131 // Set a default Content-Type if one was not supplied
132 $type = $this->getHeader($headers, 'content-type');
133 if (!$type && ($filename === '0' || $filename)) {
134 if ($type = mimetype_from_filename($filename)) {
135 $headers['Content-Type'] = $type;
139 return [$stream, $headers];
142 private function getHeader(array $headers, $key)
144 $lowercaseHeader = strtolower($key);
145 foreach ($headers as $k => $v) {
146 if (strtolower($k) === $lowercaseHeader) {