‪TYPO3CMS  ‪main
UploadedFile.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Http\Message\StreamInterface;
21 use Psr\Http\Message\UploadedFileInterface;
24 
33 class ‪UploadedFile implements UploadedFileInterface
34 {
35  protected ?string ‪$file = null;
36  protected ?StreamInterface ‪$stream = null;
37  protected ?string ‪$clientFilename;
38  protected ?string ‪$clientMediaType;
39  protected int ‪$error;
40  protected bool ‪$moved = false;
41  protected int ‪$size;
42 
54  public function ‪__construct($input, int ‪$size, int $errorStatus, ?string ‪$clientFilename = null, ?string ‪$clientMediaType = null)
55  {
56  if (is_string($input)) {
57  $this->file = $input;
58  }
59 
60  if (is_resource($input)) {
61  $this->stream = new ‪Stream($input);
62  } elseif ($input instanceof StreamInterface) {
63  $this->stream = $input;
64  }
65 
66  if (!$this->file && !$this->stream) {
67  throw new \InvalidArgumentException('The input given was not a valid stream or file.', 1436717301);
68  }
69 
70  $this->size = ‪$size;
71 
72  if ($errorStatus < 0 || $errorStatus > 8) {
73  throw new \InvalidArgumentException('Invalid error status for an uploaded file. See UPLOAD_ERR_* constant in PHP.', 1436717303);
74  }
75  $this->error = $errorStatus;
76 
77  if (‪$clientFilename !== null) {
78  ‪$clientFilename = \Normalizer::normalize(‪$clientFilename);
79  }
80  $this->clientFilename = is_string(‪$clientFilename) ? ‪$clientFilename : null;
81  $this->clientMediaType = ‪$clientMediaType;
82  }
83 
96  public function ‪getStream(): StreamInterface
97  {
98  if ($this->moved) {
99  throw new \RuntimeException('Cannot retrieve stream as it was moved.', 1436717306);
100  }
101 
102  if ($this->stream instanceof StreamInterface) {
103  return ‪$this->stream;
104  }
105 
106  $this->stream = new ‪Stream((string)$this->file);
107  return ‪$this->stream;
108  }
109 
141  public function ‪moveTo(string $targetPath): void
142  {
143  if (empty($targetPath)) {
144  throw new \InvalidArgumentException('Invalid path while moving an uploaded file.', 1436717307);
145  }
146 
147  if ($this->moved) {
148  throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717308);
149  }
150 
151  // Check if the target path is inside the allowed paths of TYPO3, and make it absolute.
152  $targetPath = GeneralUtility::getFileAbsFileName($targetPath);
153  if (empty($targetPath)) {
154  throw new \RuntimeException('Cannot move uploaded file, as it was already moved.', 1436717309);
155  }
156 
157  // Max upload size (kb) for files.
158  $maxUploadFileSize = GeneralUtility::getMaxUploadFileSize() * 1024;
159  if ($this->size > 0 && $maxUploadFileSize > 0 && $this->size >= $maxUploadFileSize) {
160  unlink($this->file);
161  throw new ‪UploadSizeException('The uploaded file exceeds the size-limit of ' . $maxUploadFileSize . ' bytes', 1647338094);
162  }
163 
164  if (!empty($this->file) && is_uploaded_file($this->file)) {
165  if (GeneralUtility::upload_copy_move($this->file, $targetPath) === false) {
166  throw new \RuntimeException('An error occurred while moving uploaded file', 1436717310);
167  }
168  } elseif ($this->stream) {
169  $handle = fopen($targetPath, 'wb+');
170  if ($handle === false) {
171  throw new \RuntimeException('Unable to write to target path.', 1436717311);
172  }
173 
174  $this->stream->rewind();
175  while (!$this->stream->eof()) {
176  fwrite($handle, $this->stream->read(4096));
177  }
178 
179  fclose($handle);
180  }
181 
182  $this->moved = true;
183  }
184 
193  public function ‪getSize(): ?int
194  {
195  return ‪$this->size;
196  }
197 
211  public function ‪getError(): int
212  {
213  return ‪$this->error;
214  }
215 
227  public function ‪getClientFilename(): ?string
228  {
230  }
231 
238  public function ‪getTemporaryFileName(): ?string
239  {
240  if ($this->moved) {
241  throw new \RuntimeException('Cannot return temporary file name, as it was already moved.', 1436717337);
242  }
243  return ‪$this->file;
244  }
245 
257  public function ‪getClientMediaType(): ?string
258  {
260  }
261 }
‪TYPO3\CMS\Core\Http\UploadedFile\getError
‪int getError()
Definition: UploadedFile.php:211
‪TYPO3\CMS\Core\Http\UploadedFile\$stream
‪StreamInterface $stream
Definition: UploadedFile.php:36
‪TYPO3\CMS\Core\Http\UploadedFile\$clientMediaType
‪string $clientMediaType
Definition: UploadedFile.php:38
‪TYPO3\CMS\Core\Http\UploadedFile\__construct
‪__construct($input, int $size, int $errorStatus, ?string $clientFilename=null, ?string $clientMediaType=null)
Definition: UploadedFile.php:54
‪TYPO3\CMS\Core\Http\UploadedFile\$moved
‪bool $moved
Definition: UploadedFile.php:40
‪TYPO3\CMS\Core\Http\UploadedFile\$error
‪int $error
Definition: UploadedFile.php:39
‪TYPO3\CMS\Core\Http\UploadedFile\$file
‪string $file
Definition: UploadedFile.php:35
‪TYPO3\CMS\Core\Resource\Exception\UploadSizeException
Definition: UploadSizeException.php:21
‪TYPO3\CMS\Core\Http\UploadedFile\getClientFilename
‪string null getClientFilename()
Definition: UploadedFile.php:227
‪TYPO3\CMS\Core\Http\UploadedFile\getStream
‪StreamInterface getStream()
Definition: UploadedFile.php:96
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Http\UploadedFile\moveTo
‪moveTo(string $targetPath)
Definition: UploadedFile.php:141
‪TYPO3\CMS\Core\Http\UploadedFile
Definition: UploadedFile.php:34
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Core\Http\UploadedFile\getClientMediaType
‪string null getClientMediaType()
Definition: UploadedFile.php:257
‪TYPO3\CMS\Core\Http\UploadedFile\getTemporaryFileName
‪getTemporaryFileName()
Definition: UploadedFile.php:238
‪TYPO3\CMS\Core\Http\UploadedFile\$size
‪int $size
Definition: UploadedFile.php:41
‪TYPO3\CMS\Core\Http\UploadedFile\$clientFilename
‪string $clientFilename
Definition: UploadedFile.php:37
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18
‪TYPO3\CMS\Core\Http\UploadedFile\getSize
‪int null getSize()
Definition: UploadedFile.php:193