File: UploadedFile.php Lines 29 to 48 |
|
29 | private $stream; |
|
30 | |
|
31 | /** |
|
32 | * @param string|resource|\Psr\Http\Message\StreamInterface $streamOrFile |
|
33 | * @param int $size UPLOAD_ERR_* constant |
|
34 | * @param int $errorCode |
|
35 | * @param string $clientFilename |
|
36 | * @param string $clientMediaType |
|
37 | */ |
| | public function __construct($streamOrFile, $size, $errorCode, $clientFilename=null, $clientMediaType=null) { |
|
39 | if($errorCode === UPLOAD_ERR_OK) { |
|
40 | if(is_string($streamOrFile)) { |
|
41 | $this->file = $streamOrFile; |
|
42 | } else if(is_resource($streamOrFile)) { |
|
43 | $this->stream = new Stream($streamOrFile); |
|
44 | } else if($streamOrFile instanceof \Psr\Http\Message\StreamInterface) { |
|
45 | $this->stream = $streamOrFile; |
|
46 | } else { |
|
47 | throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile'); |
|
48 | } |
|
File: UploadedFile.php Lines 68 to 87 |
|
68 | } |
|
69 | $this->clientMediaType = $clientMediaType; |
|
70 | } |
|
71 | |
|
72 | /** |
|
73 | * $_FILES の1件分から作成する |
|
74 | * @param array $file |
|
75 | * @return \Puyo\Psr7\UploadedFile |
|
76 | */ |
| | static public function createBy_FILE(array $file) { |
|
78 | return new UploadedFile($file['tmp_name'], $file['size'], $file['error'], $file['name'], $file['type']); |
|
79 | } |
|
80 | |
|
81 | /** |
|
82 | * {@inheritdoc} |
|
83 | * @return \Psr\Http\Message\StreamInterface |
|
84 | * @throws \RuntimeException if the upload was not successful. |
|
85 | */ |
|
86 | public function getStream() { |
|
87 | if(!$this->isUploadOk()) { |
|
File: UploadedFile.php Lines 150 to 169 |
|
150 | /** |
|
151 | * {@inheritdoc} |
|
152 | * @return string |
|
153 | */ |
|
154 | public function getClientMediaType() { |
|
155 | return $this->clientMediaType; |
|
156 | } |
|
157 | |
|
158 | private function writeFile($path) { |
| | $fp = fopen($path, 'wb+'); |
|
160 | if(!is_resource($fp)) { |
|
161 | throw new \RuntimeException('Unable to write to designated path'); |
|
162 | } |
|
163 | |
|
164 | $stream = $this->getStream(); |
|
165 | while(!$stream->eof()) { |
|
166 | fwrite($fp, $stream->read(4096)); |
|
167 | } |
|
168 | |
|
169 | fclose($fp); |
|