Open Journal Systems  3.3.0
vendor/symfony/http-foundation/File/UploadedFile.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
24 
32 class UploadedFile extends File
33 {
34  private $test;
35  private $originalName;
36  private $mimeType;
37  private $error;
38 
63  public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false)
64  {
65  $this->originalName = $this->getName($originalName);
66  $this->mimeType = $mimeType ?: 'application/octet-stream';
67 
68  if (4 < \func_num_args() ? !\is_bool($test) : null !== $error && @filesize($path) === $error) {
69  @trigger_error(sprintf('Passing a size as 4th argument to the constructor of "%s" is deprecated since Symfony 4.1.', __CLASS__), E_USER_DEPRECATED);
70  $error = $test;
71  $test = 5 < \func_num_args() ? func_get_arg(5) : false;
72  }
73 
74  $this->error = $error ?: UPLOAD_ERR_OK;
75  $this->test = $test;
76 
77  parent::__construct($path, UPLOAD_ERR_OK === $this->error);
78  }
79 
88  public function getClientOriginalName()
89  {
90  return $this->originalName;
91  }
92 
101  public function getClientOriginalExtension()
102  {
103  return pathinfo($this->originalName, PATHINFO_EXTENSION);
104  }
105 
119  public function getClientMimeType()
120  {
121  return $this->mimeType;
122  }
123 
141  public function guessClientExtension()
142  {
143  return MimeTypes::getDefault()->getExtensions($this->getClientMimeType())[0] ?? null;
144  }
145 
156  public function getClientSize()
157  {
158  @trigger_error(sprintf('The "%s()" method is deprecated since Symfony 4.1. Use getSize() instead.', __METHOD__), E_USER_DEPRECATED);
159 
160  return $this->getSize();
161  }
162 
171  public function getError()
172  {
173  return $this->error;
174  }
175 
181  public function isValid()
182  {
183  $isOk = UPLOAD_ERR_OK === $this->error;
184 
185  return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
186  }
187 
198  public function move($directory, $name = null)
199  {
200  if ($this->isValid()) {
201  if ($this->test) {
202  return parent::move($directory, $name);
203  }
204 
205  $target = $this->getTargetFile($directory, $name);
206 
207  set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
208  $moved = move_uploaded_file($this->getPathname(), $target);
209  restore_error_handler();
210  if (!$moved) {
211  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
212  }
213 
214  @chmod($target, 0666 & ~umask());
215 
216  return $target;
217  }
218 
219  switch ($this->error) {
220  case UPLOAD_ERR_INI_SIZE:
221  throw new IniSizeFileException($this->getErrorMessage());
222  case UPLOAD_ERR_FORM_SIZE:
223  throw new FormSizeFileException($this->getErrorMessage());
224  case UPLOAD_ERR_PARTIAL:
225  throw new PartialFileException($this->getErrorMessage());
226  case UPLOAD_ERR_NO_FILE:
227  throw new NoFileException($this->getErrorMessage());
228  case UPLOAD_ERR_CANT_WRITE:
229  throw new CannotWriteFileException($this->getErrorMessage());
230  case UPLOAD_ERR_NO_TMP_DIR:
231  throw new NoTmpDirFileException($this->getErrorMessage());
232  case UPLOAD_ERR_EXTENSION:
233  throw new ExtensionFileException($this->getErrorMessage());
234  }
235 
236  throw new FileException($this->getErrorMessage());
237  }
238 
244  public static function getMaxFilesize()
245  {
246  $sizePostMax = self::parseFilesize(ini_get('post_max_size'));
247  $sizeUploadMax = self::parseFilesize(ini_get('upload_max_filesize'));
248 
249  return min($sizePostMax ?: PHP_INT_MAX, $sizeUploadMax ?: PHP_INT_MAX);
250  }
251 
255  private static function parseFilesize($size): int
256  {
257  if ('' === $size) {
258  return 0;
259  }
260 
261  $size = strtolower($size);
262 
263  $max = ltrim($size, '+');
264  if (0 === strpos($max, '0x')) {
265  $max = \intval($max, 16);
266  } elseif (0 === strpos($max, '0')) {
267  $max = \intval($max, 8);
268  } else {
269  $max = (int) $max;
270  }
271 
272  switch (substr($size, -1)) {
273  case 't': $max *= 1024;
274  // no break
275  case 'g': $max *= 1024;
276  // no break
277  case 'm': $max *= 1024;
278  // no break
279  case 'k': $max *= 1024;
280  }
281 
282  return $max;
283  }
284 
290  public function getErrorMessage()
291  {
292  static $errors = [
293  UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
294  UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
295  UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
296  UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
297  UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
298  UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
299  UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
300  ];
301 
302  $errorCode = $this->error;
303  $maxFilesize = UPLOAD_ERR_INI_SIZE === $errorCode ? self::getMaxFilesize() / 1024 : 0;
304  $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
305 
306  return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
307  }
308 }
Symfony\Component\HttpFoundation\File\UploadedFile\getClientOriginalName
getClientOriginalName()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:88
Symfony\Component\HttpFoundation\File\File\getName
getName($name)
Definition: lib/vendor/symfony/http-foundation/File/File.php:128
Symfony\Component\HttpFoundation\File\Exception\PartialFileException
Definition: PartialFileException.php:19
Symfony\Component\HttpFoundation\File\Exception\IniSizeFileException
Definition: IniSizeFileException.php:19
Symfony\Component\HttpFoundation\File
Symfony\Component\HttpFoundation\File\UploadedFile\getError
getError()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:171
Symfony\Component\HttpFoundation\File\UploadedFile\guessClientExtension
guessClientExtension()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:141
Symfony\Component\HttpFoundation\File\UploadedFile\getClientSize
getClientSize()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:156
Symfony\Component\HttpFoundation\File\UploadedFile\getErrorMessage
getErrorMessage()
Definition: lib/vendor/symfony/http-foundation/File/UploadedFile.php:290
Symfony\Component\Mime\MimeTypes\getDefault
static getDefault()
Definition: MimeTypes.php:69
Symfony\Component\HttpFoundation\File\UploadedFile\move
move($directory, $name=null)
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:198
Symfony\Component\HttpFoundation\File\Exception\NoTmpDirFileException
Definition: NoTmpDirFileException.php:19
Symfony\Component\HttpFoundation\File\UploadedFile\getClientOriginalExtension
getClientOriginalExtension()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:101
Symfony\Component\Mime\MimeTypes
Definition: MimeTypes.php:37
Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileNotFoundException.php:19
Symfony\Component\HttpFoundation\File\Exception\CannotWriteFileException
Definition: CannotWriteFileException.php:19
Symfony\Component\HttpFoundation\File\Exception\ExtensionFileException
Definition: ExtensionFileException.php:19
Symfony\Component\HttpFoundation\File\File\getTargetFile
getTargetFile($directory, $name=null)
Definition: lib/vendor/symfony/http-foundation/File/File.php:106
Symfony\Component\HttpFoundation\File\UploadedFile\getMaxFilesize
static getMaxFilesize()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:244
Symfony\Component\HttpFoundation\File\Exception\NoFileException
Definition: NoFileException.php:19
Symfony\Component\HttpFoundation\File\UploadedFile\isValid
isValid()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:181
Symfony\Component\HttpFoundation\File\Exception\FileException
Definition: lib/vendor/symfony/http-foundation/File/Exception/FileException.php:19
Symfony\Component\HttpFoundation\File\UploadedFile\__construct
__construct(string $path, string $originalName, string $mimeType=null, int $error=null, $test=false)
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:63
Symfony\Component\HttpFoundation\File\UploadedFile\getClientMimeType
getClientMimeType()
Definition: vendor/symfony/http-foundation/File/UploadedFile.php:119
Symfony\Component\HttpFoundation\File\Exception\FormSizeFileException
Definition: FormSizeFileException.php:19