32 class UploadedFile
extends File
35 private $originalName;
63 public function __construct(
string $path,
string $originalName,
string $mimeType =
null,
int $error =
null, $test =
false)
65 $this->originalName = $this->
getName($originalName);
66 $this->mimeType = $mimeType ?:
'application/octet-stream';
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);
71 $test = 5 < \func_num_args() ? func_get_arg(5) :
false;
74 $this->error = $error ?: UPLOAD_ERR_OK;
77 parent::__construct($path, UPLOAD_ERR_OK === $this->error);
90 return $this->originalName;
103 return pathinfo($this->originalName, PATHINFO_EXTENSION);
121 return $this->mimeType;
158 @trigger_error(sprintf(
'The "%s()" method is deprecated since Symfony 4.1. Use getSize() instead.', __METHOD__), E_USER_DEPRECATED);
160 return $this->getSize();
183 $isOk = UPLOAD_ERR_OK === $this->error;
185 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
198 public function move($directory, $name =
null)
202 return parent::move($directory, $name);
207 set_error_handler(
function ($type, $msg) use (&$error) { $error = $msg; });
208 $moved = move_uploaded_file($this->getPathname(), $target);
209 restore_error_handler();
211 throw new FileException(sprintf(
'Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error)));
214 @chmod($target, 0666 & ~umask());
219 switch ($this->error) {
220 case UPLOAD_ERR_INI_SIZE:
222 case UPLOAD_ERR_FORM_SIZE:
224 case UPLOAD_ERR_PARTIAL:
226 case UPLOAD_ERR_NO_FILE:
228 case UPLOAD_ERR_CANT_WRITE:
230 case UPLOAD_ERR_NO_TMP_DIR:
232 case UPLOAD_ERR_EXTENSION:
246 $sizePostMax = self::parseFilesize(ini_get(
'post_max_size'));
247 $sizeUploadMax = self::parseFilesize(ini_get(
'upload_max_filesize'));
249 return min($sizePostMax ?: PHP_INT_MAX, $sizeUploadMax ?: PHP_INT_MAX);
255 private static function parseFilesize($size): int
261 $size = strtolower($size);
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);
272 switch (substr($size, -1)) {
273 case 't': $max *= 1024;
275 case 'g': $max *= 1024;
277 case 'm': $max *= 1024;
279 case 'k': $max *= 1024;
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.',
302 $errorCode = $this->error;
304 $message = isset($errors[$errorCode]) ? $errors[$errorCode] :
'The file "%s" was not uploaded due to an unknown error.';