← All changes
|
vendor/symfony/http-foundation/File/UploadedFile.php
+25
-45
3.0
→
3.5.10
View file →
| @@ -59,19 +59,12 @@ | ||
| 59 | 59 | * |
| 60 | 60 | * @throws FileException If file_uploads is disabled |
| 61 | 61 | * @throws FileNotFoundException If the file does not exist |
| 62 | 62 | */ |
| 63 | - public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false) | |
| 63 | + public function __construct(string $path, string $originalName, ?string $mimeType = null, ?int $error = null, bool $test = false) | |
| 64 | 64 | { |
| 65 | 65 | $this->originalName = $this->getName($originalName); |
| 66 | 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 | 67 | $this->error = $error ?: \UPLOAD_ERR_OK; |
| 75 | 68 | $this->test = $test; |
| 76 | 69 | |
| 77 | 70 | parent::__construct($path, \UPLOAD_ERR_OK === $this->error); |
| @@ -80,11 +73,11 @@ | ||
| 80 | 73 | /** |
| 81 | 74 | * Returns the original file name. |
| 82 | 75 | * |
| 83 | 76 | * It is extracted from the request from which the file has been uploaded. |
| 84 | - * Then it should not be considered as a safe value. | |
| 77 | + * This should not be considered as a safe value to use for a file name on your servers. | |
| 85 | 78 | * |
| 86 | - * @return string The original name | |
| 79 | + * @return string | |
| 87 | 80 | */ |
| 88 | 81 | public function getClientOriginalName() |
| 89 | 82 | { |
| 90 | 83 | return $this->originalName; |
| @@ -93,11 +86,11 @@ | ||
| 93 | 86 | /** |
| 94 | 87 | * Returns the original file extension. |
| 95 | 88 | * |
| 96 | 89 | * It is extracted from the original file name that was uploaded. |
| 97 | - * Then it should not be considered as a safe value. | |
| 90 | + * This should not be considered as a safe value to use for a file name on your servers. | |
| 98 | 91 | * |
| 99 | - * @return string The extension | |
| 92 | + * @return string | |
| 100 | 93 | */ |
| 101 | 94 | public function getClientOriginalExtension() |
| 102 | 95 | { |
| 103 | 96 | return pathinfo($this->originalName, \PATHINFO_EXTENSION); |
| @@ -111,9 +104,9 @@ | ||
| 111 | 104 | * |
| 112 | 105 | * For a trusted mime type, use getMimeType() instead (which guesses the mime |
| 113 | 106 | * type based on the file content). |
| 114 | 107 | * |
| 115 | - * @return string The mime type | |
| 108 | + * @return string | |
| 116 | 109 | * |
| 117 | 110 | * @see getMimeType() |
| 118 | 111 | */ |
| 119 | 112 | public function getClientMimeType() |
| @@ -132,9 +125,9 @@ | ||
| 132 | 125 | * |
| 133 | 126 | * For a trusted extension, use guessExtension() instead (which guesses |
| 134 | 127 | * the extension based on the guessed mime type for the file). |
| 135 | 128 | * |
| 136 | - * @return string|null The guessed extension or null if it cannot be guessed | |
| 129 | + * @return string|null | |
| 137 | 130 | * |
| 138 | 131 | * @see guessExtension() |
| 139 | 132 | * @see getClientMimeType() |
| 140 | 133 | */ |
| @@ -139,35 +132,22 @@ | ||
| 139 | 132 | * @see getClientMimeType() |
| 140 | 133 | */ |
| 141 | 134 | public function guessClientExtension() |
| 142 | 135 | { |
| 136 | + if (!class_exists(MimeTypes::class)) { | |
| 137 | + throw new \LogicException('You cannot guess the extension as the Mime component is not installed. Try running "composer require symfony/mime".'); | |
| 138 | + } | |
| 139 | + | |
| 143 | 140 | return MimeTypes::getDefault()->getExtensions($this->getClientMimeType())[0] ?? null; |
| 144 | 141 | } |
| 145 | 142 | |
| 146 | 143 | /** |
| 147 | - * Returns the file size. | |
| 148 | - * | |
| 149 | - * It is extracted from the request from which the file has been uploaded. | |
| 150 | - * Then it should not be considered as a safe value. | |
| 151 | - * | |
| 152 | - * @deprecated since Symfony 4.1, use getSize() instead. | |
| 153 | - * | |
| 154 | - * @return int|null The file sizes | |
| 155 | - */ | |
| 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 | - | |
| 163 | - /** | |
| 164 | 144 | * Returns the upload error. |
| 165 | 145 | * |
| 166 | 146 | * If the upload was successful, the constant UPLOAD_ERR_OK is returned. |
| 167 | 147 | * Otherwise one of the other UPLOAD_ERR_XXX constants is returned. |
| 168 | 148 | * |
| 169 | - * @return int The upload error | |
| 149 | + * @return int | |
| 170 | 150 | */ |
| 171 | 151 | public function getError() |
| 172 | 152 | { |
| 173 | 153 | return $this->error; |
| @@ -173,11 +153,11 @@ | ||
| 173 | 153 | return $this->error; |
| 174 | 154 | } |
| 175 | 155 | |
| 176 | 156 | /** |
| 177 | - * Returns whether the file was uploaded successfully. | |
| 157 | + * Returns whether the file has been uploaded with HTTP and no error occurred. | |
| 178 | 158 | * |
| 179 | - * @return bool True if the file has been uploaded with HTTP and no error occurred | |
| 159 | + * @return bool | |
| 180 | 160 | */ |
| 181 | 161 | public function isValid() |
| 182 | 162 | { |
| 183 | 163 | $isOk = \UPLOAD_ERR_OK === $this->error; |
| @@ -187,16 +167,13 @@ | ||
| 187 | 167 | |
| 188 | 168 | /** |
| 189 | 169 | * Moves the file to a new location. |
| 190 | 170 | * |
| 191 | - * @param string $directory The destination folder | |
| 192 | - * @param string $name The new file name | |
| 171 | + * @return File | |
| 193 | 172 | * |
| 194 | - * @return File A File object representing the new file | |
| 195 | - * | |
| 196 | 173 | * @throws FileException if, for any reason, the file could not have been moved |
| 197 | 174 | */ |
| 198 | - public function move($directory, $name = null) | |
| 175 | + public function move(string $directory, ?string $name = null) | |
| 199 | 176 | { |
| 200 | 177 | if ($this->isValid()) { |
| 201 | 178 | if ($this->test) { |
| 202 | 179 | return parent::move($directory, $name); |
| @@ -204,10 +181,13 @@ | ||
| 204 | 181 | |
| 205 | 182 | $target = $this->getTargetFile($directory, $name); |
| 206 | 183 | |
| 207 | 184 | set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; }); |
| 208 | - $moved = move_uploaded_file($this->getPathname(), $target); | |
| 209 | - restore_error_handler(); | |
| 185 | + try { | |
| 186 | + $moved = move_uploaded_file($this->getPathname(), $target); | |
| 187 | + } finally { | |
| 188 | + restore_error_handler(); | |
| 189 | + } | |
| 210 | 190 | if (!$moved) { |
| 211 | 191 | throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s).', $this->getPathname(), $target, strip_tags($error))); |
| 212 | 192 | } |
| 213 | 193 | |
| @@ -272,13 +252,13 @@ | ||
| 272 | 252 | } |
| 273 | 253 | |
| 274 | 254 | switch (substr($size, -1)) { |
| 275 | 255 | case 't': $max *= 1024; |
| 276 | - // no break | |
| 256 | + // no break | |
| 277 | 257 | case 'g': $max *= 1024; |
| 278 | - // no break | |
| 258 | + // no break | |
| 279 | 259 | case 'm': $max *= 1024; |
| 280 | - // no break | |
| 260 | + // no break | |
| 281 | 261 | case 'k': $max *= 1024; |
| 282 | 262 | } |
| 283 | 263 | |
| 284 | 264 | return $max; |
| @@ -286,9 +266,9 @@ | ||
| 286 | 266 | |
| 287 | 267 | /** |
| 288 | 268 | * Returns an informative upload error message. |
| 289 | 269 | * |
| 290 | - * @return string The error message regarding the specified error code | |
| 270 | + * @return string | |
| 291 | 271 | */ |
| 292 | 272 | public function getErrorMessage() |
| 293 | 273 | { |
| 294 | 274 | static $errors = [ |