30 $this->boundary =
$boundary ?: sha1(uniqid(
'',
true));
55 foreach ($headers as
$key => $value) {
56 $str .=
"{$key}: {$value}\r\n";
59 return "--{$this->boundary}\r\n" . trim($str) .
"\r\n\r\n";
69 foreach ($elements as $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;
99 isset($element[
'filename']) ? $element[
'filename'] : null,
100 isset($element[
'headers']) ? $element[
'headers'] : []
114 $disposition = $this->
getHeader($headers,
'content-disposition');
117 ? sprintf(
'form-data; name="%s"; filename="%s"',
120 :
"form-data; name=\"{$name}\"";
124 $length = $this->
getHeader($headers,
'content-length');
126 if ($length = $stream->
getSize()) {
127 $headers[
'Content-Length'] = (string) $length;
135 $headers[
'Content-Type'] =
$type;
144 $lowercaseHeader = strtolower(
$key);
145 foreach ($headers as $k => $v) {
146 if (strtolower($k) === $lowercaseHeader) {
isWritable()
Returns whether or not the stream is writable.
getHeader(array $headers, $key)
mimetype_from_filename($filename)
Determines the mimetype of a file by looking at its extension.
$stream
PHP stream implementation.
stream_for($resource='', array $options=[])
Create a new stream based on the input type.
getHeaders(array $headers)
Get the headers needed before transferring the content of a POST file.
__construct(array $elements=[], $boundary=null)
addStream(StreamInterface $stream)
Add a stream to the AppendStream.
getBoundary()
Get the boundary.
getSize()
Get the size of the stream if known.
createElement($name, StreamInterface $stream, $filename, array $headers)
createStream(array $elements)
Create the aggregate stream that will be used to upload the POST data.
addElement(AppendStream $stream, array $element)
Reads from multiple streams, one after the other.
Stream that when read returns bytes for a streaming multipart or multipart/form-data stream...