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