Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
multipartstream.php
1<?php
2
4
6
8{
9 protected const BUF_LEN = 524288;
10
11 protected $boundary;
12
13 public function getBoundary(): string
14 {
15 if ($this->boundary === null)
16 {
17 $this->boundary = 'BXC' . uniqid('', true);
18 }
19
20 return $this->boundary;
21 }
22
23 protected function build(array $data)
24 {
25 $boundary = $this->getBoundary();
26
27 foreach ($data as $k => $v)
28 {
29 $this->write('--' . $boundary . "\r\n");
30
31 if ((is_resource($v) && get_resource_type($v) === 'stream') || is_array($v))
32 {
33 $filename = $v['filename'] ?? $k;
34 $contentType = $v['contentType'] ?? 'application/octet-stream';
35
36 $this->write('Content-Disposition: form-data; name="' . $k . '"; filename="' . $filename . '"' . "\r\n");
37 $this->write('Content-Type: ' . $contentType . "\r\n\r\n");
38
39 if (is_array($v))
40 {
41 if (isset($v['resource']) && is_resource($v['resource']) && get_resource_type($v['resource']) === 'stream')
42 {
43 fseek($v['resource'], 0);
44 while (!feof($v['resource']))
45 {
46 $this->write(stream_get_contents($v['resource'], static::BUF_LEN));
47 }
48 }
49 else
50 {
51 if (isset($v['content']))
52 {
53 $this->write($v['content']);
54 }
55 else
56 {
57 throw new ArgumentException("File `{$k}` not found for multipart upload.", 'data');
58 }
59 }
60
61 }
62 else
63 {
64 fseek($v, 0);
65 while (!feof($v))
66 {
67 $this->write(stream_get_contents($v, static::BUF_LEN));
68 }
69 }
70 }
71 else
72 {
73 $this->write('Content-Disposition: form-data; name="' . $k . '"' . "\r\n\r\n");
74 $this->write($v);
75 }
76
77 $this->write("\r\n");
78 }
79
80 $this->write('--' . $boundary . "--\r\n");
81 }
82}
write(string $string)
Definition stream.php:187