No Description

UploadedFile.php 8.9KB

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