ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
UploadedFile.php
Go to the documentation of this file.
1 <?php
2 namespace GuzzleHttp\Psr7;
3 
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 
28  private $clientFilename;
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  {
306  return $this->clientFilename;
307  }
308 
312  public function getClientMediaType()
313  {
314  return $this->clientMediaType;
315  }
316 }
setStreamOrFile($streamOrFile)
Depending on the value set file or stream variable.
moveTo($targetPath)
{Move the uploaded file to a new location.Use this method as an alternative to move_uploaded_file(). This method is guaranteed to work in both SAPI and non-SAPI environments. Implementations must determine which environment they are in, and use the appropriate method (move_uploaded_file(), rename(), or a stream operation) to perform the operation.$targetPath may be an absolute path, or a relative path. If it is a relative path, resolution should be the same as used by PHP&#39;s rename() function.The original file or stream MUST be removed on completion.If this method is called more than once, any subsequent calls MUST raise an exception.When used in an SAPI environment where $_FILES is populated, when writing files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be used to ensure permissions and upload status are verified correctly.If you wish to move to a stream, use getStream(), as SAPI operations cannot guarantee writing to stream destinations.http://php.net/is_uploaded_file http://php.net/move_uploaded_file Path to which to move the uploaded file. }
getSize()
{Retrieve the file size.Implementations SHOULD return the value stored in the "size" key of the file ...
isOk()
Return true if there is no upload error.
getClientFilename()
{Retrieve the filename sent by the client.Do not trust the value returned by this method...
setClientMediaType($clientMediaType)
__construct( $streamOrFile, $size, $errorStatus, $clientFilename=null, $clientMediaType=null)
getError()
{Retrieve the error associated with the uploaded file.The return value MUST be one of PHP&#39;s UPLOAD_ER...
getStream()
{Retrieve a stream representing the uploaded file.This method MUST return a StreamInterface instance...
getClientMediaType()
{Retrieve the media type sent by the client.Do not trust the value returned by this method...
Lazily reads or writes to a file that is opened only after an IO operation take place on the stream...
setClientFilename($clientFilename)
font size
Definition: langcheck.php:162
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
Value object representing a file uploaded through an HTTP request.
Describes a data stream.