annotate vendor/symfony/http-foundation/File/UploadedFile.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\HttpFoundation\File;
Chris@0 13
Chris@0 14 use Symfony\Component\HttpFoundation\File\Exception\FileException;
Chris@0 15 use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
Chris@0 16 use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
Chris@0 17
Chris@0 18 /**
Chris@0 19 * A file uploaded through a form.
Chris@0 20 *
Chris@0 21 * @author Bernhard Schussek <bschussek@gmail.com>
Chris@0 22 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 */
Chris@0 25 class UploadedFile extends File
Chris@0 26 {
Chris@0 27 /**
Chris@0 28 * Whether the test mode is activated.
Chris@0 29 *
Chris@0 30 * Local files are used in test mode hence the code should not enforce HTTP uploads.
Chris@0 31 *
Chris@0 32 * @var bool
Chris@0 33 */
Chris@0 34 private $test = false;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The original name of the uploaded file.
Chris@0 38 *
Chris@0 39 * @var string
Chris@0 40 */
Chris@0 41 private $originalName;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * The mime type provided by the uploader.
Chris@0 45 *
Chris@0 46 * @var string
Chris@0 47 */
Chris@0 48 private $mimeType;
Chris@0 49
Chris@0 50 /**
Chris@0 51 * The file size provided by the uploader.
Chris@0 52 *
Chris@0 53 * @var int|null
Chris@0 54 */
Chris@0 55 private $size;
Chris@0 56
Chris@0 57 /**
Chris@0 58 * The UPLOAD_ERR_XXX constant provided by the uploader.
Chris@0 59 *
Chris@0 60 * @var int
Chris@0 61 */
Chris@0 62 private $error;
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
Chris@0 66 *
Chris@0 67 * The file object is only created when the uploaded file is valid (i.e. when the
Chris@0 68 * isValid() method returns true). Otherwise the only methods that could be called
Chris@0 69 * on an UploadedFile instance are:
Chris@0 70 *
Chris@0 71 * * getClientOriginalName,
Chris@0 72 * * getClientMimeType,
Chris@0 73 * * isValid,
Chris@0 74 * * getError.
Chris@0 75 *
Chris@0 76 * Calling any other method on an non-valid instance will cause an unpredictable result.
Chris@0 77 *
Chris@0 78 * @param string $path The full temporary path to the file
Chris@0 79 * @param string $originalName The original file name
Chris@0 80 * @param string|null $mimeType The type of the file as provided by PHP; null defaults to application/octet-stream
Chris@0 81 * @param int|null $size The file size
Chris@0 82 * @param int|null $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
Chris@0 83 * @param bool $test Whether the test mode is active
Chris@0 84 *
Chris@0 85 * @throws FileException If file_uploads is disabled
Chris@0 86 * @throws FileNotFoundException If the file does not exist
Chris@0 87 */
Chris@0 88 public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
Chris@0 89 {
Chris@0 90 $this->originalName = $this->getName($originalName);
Chris@0 91 $this->mimeType = $mimeType ?: 'application/octet-stream';
Chris@0 92 $this->size = $size;
Chris@0 93 $this->error = $error ?: UPLOAD_ERR_OK;
Chris@0 94 $this->test = (bool) $test;
Chris@0 95
Chris@0 96 parent::__construct($path, UPLOAD_ERR_OK === $this->error);
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Returns the original file name.
Chris@0 101 *
Chris@0 102 * It is extracted from the request from which the file has been uploaded.
Chris@0 103 * Then it should not be considered as a safe value.
Chris@0 104 *
Chris@0 105 * @return string|null The original name
Chris@0 106 */
Chris@0 107 public function getClientOriginalName()
Chris@0 108 {
Chris@0 109 return $this->originalName;
Chris@0 110 }
Chris@0 111
Chris@0 112 /**
Chris@0 113 * Returns the original file extension.
Chris@0 114 *
Chris@0 115 * It is extracted from the original file name that was uploaded.
Chris@0 116 * Then it should not be considered as a safe value.
Chris@0 117 *
Chris@0 118 * @return string The extension
Chris@0 119 */
Chris@0 120 public function getClientOriginalExtension()
Chris@0 121 {
Chris@0 122 return pathinfo($this->originalName, PATHINFO_EXTENSION);
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Returns the file mime type.
Chris@0 127 *
Chris@0 128 * The client mime type is extracted from the request from which the file
Chris@0 129 * was uploaded, so it should not be considered as a safe value.
Chris@0 130 *
Chris@0 131 * For a trusted mime type, use getMimeType() instead (which guesses the mime
Chris@0 132 * type based on the file content).
Chris@0 133 *
Chris@0 134 * @return string|null The mime type
Chris@0 135 *
Chris@0 136 * @see getMimeType()
Chris@0 137 */
Chris@0 138 public function getClientMimeType()
Chris@0 139 {
Chris@0 140 return $this->mimeType;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * Returns the extension based on the client mime type.
Chris@0 145 *
Chris@0 146 * If the mime type is unknown, returns null.
Chris@0 147 *
Chris@0 148 * This method uses the mime type as guessed by getClientMimeType()
Chris@0 149 * to guess the file extension. As such, the extension returned
Chris@0 150 * by this method cannot be trusted.
Chris@0 151 *
Chris@0 152 * For a trusted extension, use guessExtension() instead (which guesses
Chris@0 153 * the extension based on the guessed mime type for the file).
Chris@0 154 *
Chris@0 155 * @return string|null The guessed extension or null if it cannot be guessed
Chris@0 156 *
Chris@0 157 * @see guessExtension()
Chris@0 158 * @see getClientMimeType()
Chris@0 159 */
Chris@0 160 public function guessClientExtension()
Chris@0 161 {
Chris@0 162 $type = $this->getClientMimeType();
Chris@0 163 $guesser = ExtensionGuesser::getInstance();
Chris@0 164
Chris@0 165 return $guesser->guess($type);
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Returns the file size.
Chris@0 170 *
Chris@0 171 * It is extracted from the request from which the file has been uploaded.
Chris@0 172 * Then it should not be considered as a safe value.
Chris@0 173 *
Chris@0 174 * @return int|null The file size
Chris@0 175 */
Chris@0 176 public function getClientSize()
Chris@0 177 {
Chris@0 178 return $this->size;
Chris@0 179 }
Chris@0 180
Chris@0 181 /**
Chris@0 182 * Returns the upload error.
Chris@0 183 *
Chris@0 184 * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
Chris@0 185 * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
Chris@0 186 *
Chris@0 187 * @return int The upload error
Chris@0 188 */
Chris@0 189 public function getError()
Chris@0 190 {
Chris@0 191 return $this->error;
Chris@0 192 }
Chris@0 193
Chris@0 194 /**
Chris@0 195 * Returns whether the file was uploaded successfully.
Chris@0 196 *
Chris@0 197 * @return bool True if the file has been uploaded with HTTP and no error occurred
Chris@0 198 */
Chris@0 199 public function isValid()
Chris@0 200 {
Chris@0 201 $isOk = $this->error === UPLOAD_ERR_OK;
Chris@0 202
Chris@0 203 return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
Chris@0 204 }
Chris@0 205
Chris@0 206 /**
Chris@0 207 * Moves the file to a new location.
Chris@0 208 *
Chris@0 209 * @param string $directory The destination folder
Chris@0 210 * @param string $name The new file name
Chris@0 211 *
Chris@0 212 * @return File A File object representing the new file
Chris@0 213 *
Chris@0 214 * @throws FileException if, for any reason, the file could not have been moved
Chris@0 215 */
Chris@0 216 public function move($directory, $name = null)
Chris@0 217 {
Chris@0 218 if ($this->isValid()) {
Chris@0 219 if ($this->test) {
Chris@0 220 return parent::move($directory, $name);
Chris@0 221 }
Chris@0 222
Chris@0 223 $target = $this->getTargetFile($directory, $name);
Chris@0 224
Chris@0 225 if (!@move_uploaded_file($this->getPathname(), $target)) {
Chris@0 226 $error = error_get_last();
Chris@0 227 throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
Chris@0 228 }
Chris@0 229
Chris@0 230 @chmod($target, 0666 & ~umask());
Chris@0 231
Chris@0 232 return $target;
Chris@0 233 }
Chris@0 234
Chris@0 235 throw new FileException($this->getErrorMessage());
Chris@0 236 }
Chris@0 237
Chris@0 238 /**
Chris@0 239 * Returns the maximum size of an uploaded file as configured in php.ini.
Chris@0 240 *
Chris@0 241 * @return int The maximum size of an uploaded file in bytes
Chris@0 242 */
Chris@0 243 public static function getMaxFilesize()
Chris@0 244 {
Chris@0 245 $iniMax = strtolower(ini_get('upload_max_filesize'));
Chris@0 246
Chris@0 247 if ('' === $iniMax) {
Chris@0 248 return PHP_INT_MAX;
Chris@0 249 }
Chris@0 250
Chris@0 251 $max = ltrim($iniMax, '+');
Chris@0 252 if (0 === strpos($max, '0x')) {
Chris@0 253 $max = intval($max, 16);
Chris@0 254 } elseif (0 === strpos($max, '0')) {
Chris@0 255 $max = intval($max, 8);
Chris@0 256 } else {
Chris@0 257 $max = (int) $max;
Chris@0 258 }
Chris@0 259
Chris@0 260 switch (substr($iniMax, -1)) {
Chris@0 261 case 't': $max *= 1024;
Chris@0 262 case 'g': $max *= 1024;
Chris@0 263 case 'm': $max *= 1024;
Chris@0 264 case 'k': $max *= 1024;
Chris@0 265 }
Chris@0 266
Chris@0 267 return $max;
Chris@0 268 }
Chris@0 269
Chris@0 270 /**
Chris@0 271 * Returns an informative upload error message.
Chris@0 272 *
Chris@0 273 * @return string The error message regarding the specified error code
Chris@0 274 */
Chris@0 275 public function getErrorMessage()
Chris@0 276 {
Chris@0 277 static $errors = array(
Chris@0 278 UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
Chris@0 279 UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
Chris@0 280 UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
Chris@0 281 UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
Chris@0 282 UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
Chris@0 283 UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
Chris@0 284 UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
Chris@0 285 );
Chris@0 286
Chris@0 287 $errorCode = $this->error;
Chris@0 288 $maxFilesize = $errorCode === UPLOAD_ERR_INI_SIZE ? self::getMaxFilesize() / 1024 : 0;
Chris@0 289 $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
Chris@0 290
Chris@0 291 return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
Chris@0 292 }
Chris@0 293 }