‪TYPO3CMS  10.4
UploadedFile.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
16 namespace ‪TYPO3\CMS\Core\Http;
17 
18 use Psr\Http\Message\StreamInterface;
19 use Psr\Http\Message\UploadedFileInterface;
21 
30 class ‪UploadedFile implements UploadedFileInterface
31 {
35  protected ‪$file;
36 
40  protected ‪$stream;
41 
45  protected ‪$clientFilename;
46 
51 
55  protected ‪$error;
56 
60  protected ‪$moved = false;
61 
65  protected ‪$size;
66 
78  public function ‪__construct($input, ‪$size, $errorStatus, ‪$clientFilename = null, ‪$clientMediaType = null)
79  {
80  if (is_string($input)) {
81  $this->file = $input;
82  }
83 
84  if (is_resource($input)) {
85  $this->stream = new ‪Stream($input);
86  } elseif ($input instanceof StreamInterface) {
87  $this->stream = $input;
88  }
89 
90  if (!$this->file && !$this->stream) {
91  throw new \InvalidArgumentException('The input given was not a valid stream or file.', 1436717301);
92  }
93 
94  if (!is_int(‪$size)) {
95  throw new \InvalidArgumentException('The size provided for an uploaded file must be an integer.', 1436717302);
96  }
97  $this->size = ‪$size;
98 
99  if (!is_int($errorStatus) || 0 > $errorStatus || 8 < $errorStatus) {
100  throw new \InvalidArgumentException('Invalid error status for an uploaded file. See UPLOAD_ERR_* constant in PHP.', 1436717303);
101  }
102  $this->error = $errorStatus;
103 
104  if (‪$clientFilename !== null && !is_string(‪$clientFilename)) {
105  throw new \InvalidArgumentException('Invalid client filename provided for an uploaded file.', 1436717304);
106  }
107  $this->clientFilename = ‪$clientFilename;
108 
109  if (‪$clientMediaType !== null && !is_string(‪$clientMediaType)) {
110  throw new \InvalidArgumentException('Invalid client media type provided for an uploaded file.', 1436717305);
111  }
112  $this->clientMediaType = ‪$clientMediaType;
113  }
114 
127  public function ‪getStream()
128  {
129  if ($this->moved) {
130  throw new \RuntimeException('Cannot retrieve stream as it was moved.', 1436717306);
131  }
132 
133  if ($this->stream instanceof StreamInterface) {
134  return ‪$this->stream;
135  }
136 
137  $this->stream = new Stream((string)$this->file);
138  return ‪$this->stream;
139  }
140 
172  public function ‪moveTo($targetPath)
173  {
174  if (!is_string($targetPath) || empty($targetPath)) {
175  throw new \InvalidArgumentException('Invalid path while moving an uploaded file.', 1436717307);
176  }
177 
178  if ($this->moved) {
179  throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717308);
180  }
181 
182  // Check if the target path is inside the allowed paths of TYPO3, and make it absolute.
183  $targetPath = GeneralUtility::getFileAbsFileName($targetPath);
184  if (empty($targetPath)) {
185  throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717309);
186  }
187 
188  if (!empty($this->file) && is_uploaded_file($this->file)) {
189  if (GeneralUtility::upload_copy_move($this->file, $targetPath) === false) {
190  throw new \RuntimeException('An error occurred while moving uploaded file', 1436717310);
191  }
192  } elseif ($this->stream) {
193  $handle = fopen($targetPath, 'wb+');
194  if ($handle === false) {
195  throw new \RuntimeException('Unable to write to target path.', 1436717311);
196  }
197 
198  $this->stream->rewind();
199  while (!$this->stream->eof()) {
200  fwrite($handle, $this->stream->read(4096));
201  }
202 
203  fclose($handle);
204  }
205 
206  $this->moved = true;
207  }
208 
217  public function ‪getSize()
218  {
219  return ‪$this->size;
220  }
221 
235  public function ‪getError()
236  {
237  return ‪$this->error;
238  }
239 
251  public function ‪getClientFilename()
252  {
254  }
255 
267  public function ‪getClientMediaType()
268  {
270  }
271 }
‪TYPO3\CMS\Core\Http\UploadedFile\__construct
‪__construct($input, $size, $errorStatus, $clientFilename=null, $clientMediaType=null)
Definition: UploadedFile.php:71
‪TYPO3\CMS\Core\Http\UploadedFile\getError
‪int getError()
Definition: UploadedFile.php:228
‪TYPO3\CMS\Core\Http\UploadedFile\$clientMediaType
‪string $clientMediaType
Definition: UploadedFile.php:46
‪TYPO3\CMS\Core\Http\UploadedFile\$stream
‪StreamInterface null $stream
Definition: UploadedFile.php:38
‪TYPO3\CMS\Core\Http\UploadedFile\$moved
‪bool $moved
Definition: UploadedFile.php:54
‪TYPO3\CMS\Core\Http\UploadedFile\$error
‪int $error
Definition: UploadedFile.php:50
‪TYPO3\CMS\Core\Http\UploadedFile\getClientFilename
‪string null getClientFilename()
Definition: UploadedFile.php:244
‪TYPO3\CMS\Core\Http\UploadedFile\getStream
‪StreamInterface getStream()
Definition: UploadedFile.php:120
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:31
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Http\UploadedFile\getClientMediaType
‪string null getClientMediaType()
Definition: UploadedFile.php:260
‪TYPO3\CMS\Core\Http\UploadedFile\$size
‪int $size
Definition: UploadedFile.php:58
‪TYPO3\CMS\Core\Http\UploadedFile\$clientFilename
‪string $clientFilename
Definition: UploadedFile.php:42
‪TYPO3\CMS\Core\Http\UploadedFile\$file
‪string null $file
Definition: UploadedFile.php:34
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18
‪TYPO3\CMS\Core\Http\UploadedFile\moveTo
‪moveTo($targetPath)
Definition: UploadedFile.php:165
‪TYPO3\CMS\Core\Http\UploadedFile\getSize
‪int null getSize()
Definition: UploadedFile.php:210