ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
UploadedFile.php
Go to the documentation of this file.
1<?php
2namespace GuzzleHttp\Psr7;
3
4use InvalidArgumentException;
7use RuntimeException;
8
10{
14 private static $errors = [
15 UPLOAD_ERR_OK,
16 UPLOAD_ERR_INI_SIZE,
17 UPLOAD_ERR_FORM_SIZE,
18 UPLOAD_ERR_PARTIAL,
19 UPLOAD_ERR_NO_FILE,
20 UPLOAD_ERR_NO_TMP_DIR,
21 UPLOAD_ERR_CANT_WRITE,
22 UPLOAD_ERR_EXTENSION,
23 ];
24
29
34
38 private $error;
39
43 private $file;
44
48 private $moved = false;
49
53 private $size;
54
58 private $stream;
59
67 public function __construct(
68 $streamOrFile,
69 $size,
70 $errorStatus,
71 $clientFilename = null,
72 $clientMediaType = null
73 ) {
74 $this->setError($errorStatus);
75 $this->setSize($size);
78
79 if ($this->isOk()) {
80 $this->setStreamOrFile($streamOrFile);
81 }
82 }
83
90 private function setStreamOrFile($streamOrFile)
91 {
92 if (is_string($streamOrFile)) {
93 $this->file = $streamOrFile;
94 } elseif (is_resource($streamOrFile)) {
95 $this->stream = new Stream($streamOrFile);
96 } elseif ($streamOrFile instanceof StreamInterface) {
97 $this->stream = $streamOrFile;
98 } else {
99 throw new InvalidArgumentException(
100 'Invalid stream or file provided for UploadedFile'
101 );
102 }
103 }
104
109 private function setError($error)
110 {
111 if (false === is_int($error)) {
112 throw new InvalidArgumentException(
113 'Upload file error status must be an integer'
114 );
115 }
116
117 if (false === in_array($error, UploadedFile::$errors)) {
118 throw new InvalidArgumentException(
119 'Invalid error status for UploadedFile'
120 );
121 }
122
123 $this->error = $error;
124 }
125
130 private function setSize($size)
131 {
132 if (false === is_int($size)) {
133 throw new InvalidArgumentException(
134 'Upload file size must be an integer'
135 );
136 }
137
138 $this->size = $size;
139 }
140
145 private function isStringOrNull($param)
146 {
147 return in_array(gettype($param), ['string', 'NULL']);
148 }
149
154 private function isStringNotEmpty($param)
155 {
156 return is_string($param) && false === empty($param);
157 }
158
164 {
165 if (false === $this->isStringOrNull($clientFilename)) {
166 throw new InvalidArgumentException(
167 'Upload file client filename must be a string or null'
168 );
169 }
170
171 $this->clientFilename = $clientFilename;
172 }
173
179 {
180 if (false === $this->isStringOrNull($clientMediaType)) {
181 throw new InvalidArgumentException(
182 'Upload file client media type must be a string or null'
183 );
184 }
185
186 $this->clientMediaType = $clientMediaType;
187 }
188
194 private function isOk()
195 {
196 return $this->error === UPLOAD_ERR_OK;
197 }
198
202 public function isMoved()
203 {
204 return $this->moved;
205 }
206
210 private function validateActive()
211 {
212 if (false === $this->isOk()) {
213 throw new RuntimeException('Cannot retrieve stream due to upload error');
214 }
215
216 if ($this->isMoved()) {
217 throw new RuntimeException('Cannot retrieve stream after it has already been moved');
218 }
219 }
220
225 public function getStream()
226 {
227 $this->validateActive();
228
229 if ($this->stream instanceof StreamInterface) {
230 return $this->stream;
231 }
232
233 return new LazyOpenStream($this->file, 'r+');
234 }
235
247 public function moveTo($targetPath)
248 {
249 $this->validateActive();
250
251 if (false === $this->isStringNotEmpty($targetPath)) {
252 throw new InvalidArgumentException(
253 'Invalid path provided for move operation; must be a non-empty string'
254 );
255 }
256
257 if ($this->file) {
258 $this->moved = php_sapi_name() == 'cli'
259 ? rename($this->file, $targetPath)
260 : move_uploaded_file($this->file, $targetPath);
261 } else {
263 $this->getStream(),
264 new LazyOpenStream($targetPath, 'w')
265 );
266
267 $this->moved = true;
268 }
269
270 if (false === $this->moved) {
271 throw new RuntimeException(
272 sprintf('Uploaded file could not be moved to %s', $targetPath)
273 );
274 }
275 }
276
282 public function getSize()
283 {
284 return $this->size;
285 }
286
293 public function getError()
294 {
295 return $this->error;
296 }
297
304 public function getClientFilename()
305 {
307 }
308
312 public function getClientMediaType()
313 {
315 }
316}
sprintf('%.4f', $callTime)
An exception for terminatinating execution or to throw for unit testing.
Lazily reads or writes to a file that is opened only after an IO operation take place on the stream.
__construct( $streamOrFile, $size, $errorStatus, $clientFilename=null, $clientMediaType=null)
setStreamOrFile($streamOrFile)
Depending on the value set file or stream variable.
setClientFilename($clientFilename)
isOk()
Return true if there is no upload error.
getStream()
{Retrieve a stream representing the uploaded file.This method MUST return a StreamInterface instance,...
moveTo($targetPath)
{Move the uploaded file to a new location.Use this method as an alternative to move_uploaded_file()....
getClientFilename()
{Retrieve the filename sent by the client.Do not trust the value returned by this method....
setClientMediaType($clientMediaType)
getError()
{Retrieve the error associated with the uploaded file.The return value MUST be one of PHP's UPLOAD_ER...
getSize()
{Retrieve the file size.Implementations SHOULD return the value stored in the "size" key of the file ...
getClientMediaType()
{Retrieve the media type sent by the client.Do not trust the value returned by this method....
error($a_errmsg)
set error message @access public
Describes a data stream.
Value object representing a file uploaded through an HTTP request.
copy_to_stream(StreamInterface $source, StreamInterface $dest, $maxLen=-1)
Copy the contents of a stream into another stream until the given number of bytes have been read.
Definition: functions.php:369