PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Symfony/Component/HttpFoundation/File/UploadedFile.php

https://github.com/stepanets/symfony
PHP | 249 lines | 78 code | 29 blank | 142 comment | 7 complexity | 1cd71c3dd4848cb4363dfc6041c51c35 MD5 | raw file
  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. /**
  14. * A file uploaded through a form.
  15. *
  16. * @author Bernhard Schussek <bschussek@gmail.com>
  17. * @author Florian Eckerstorfer <florian@eckerstorfer.org>
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @api
  21. */
  22. class UploadedFile extends File
  23. {
  24. /**
  25. * Whether the test mode is activated.
  26. *
  27. * Local files are used in test mode hence the code should not enforce HTTP uploads.
  28. *
  29. * @var Boolean
  30. */
  31. private $test = false;
  32. /**
  33. * The original name of the uploaded file.
  34. *
  35. * @var string
  36. */
  37. private $originalName;
  38. /**
  39. * The mime type provided by the uploader.
  40. *
  41. * @var string
  42. */
  43. private $mimeType;
  44. /**
  45. * The file size provided by the uploader.
  46. *
  47. * @var string
  48. */
  49. private $size;
  50. /**
  51. * The UPLOAD_ERR_XXX constant provided by the uploader.
  52. *
  53. * @var integer
  54. */
  55. private $error;
  56. /**
  57. * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
  58. *
  59. * The file object is only created when the uploaded file is valid (i.e. when the
  60. * isValid() method returns true). Otherwise the only methods that could be called
  61. * on an UploadedFile instance are:
  62. *
  63. * * getClientOriginalName,
  64. * * getClientMimeType,
  65. * * isValid,
  66. * * getError.
  67. *
  68. * Calling any other method on an non-valid instance will cause an unpredictable result.
  69. *
  70. * @param string $path The full temporary path to the file
  71. * @param string $originalName The original file name
  72. * @param string $mimeType The type of the file as provided by PHP
  73. * @param integer $size The file size
  74. * @param integer $error The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants)
  75. * @param Boolean $test Whether the test mode is active
  76. *
  77. * @throws FileException If file_uploads is disabled
  78. * @throws FileNotFoundException If the file does not exist
  79. *
  80. * @api
  81. */
  82. public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
  83. {
  84. if (!ini_get('file_uploads')) {
  85. throw new FileException(sprintf('Unable to create UploadedFile because "file_uploads" is disabled in your php.ini file (%s)', get_cfg_var('cfg_file_path')));
  86. }
  87. $this->originalName = $this->getName($originalName);
  88. $this->mimeType = $mimeType ?: 'application/octet-stream';
  89. $this->size = $size;
  90. $this->error = $error ?: UPLOAD_ERR_OK;
  91. $this->test = (Boolean) $test;
  92. parent::__construct($path, UPLOAD_ERR_OK === $this->error);
  93. }
  94. /**
  95. * Returns the original file name.
  96. *
  97. * It is extracted from the request from which the file has been uploaded.
  98. * Then is should not be considered as a safe value.
  99. *
  100. * @return string|null The original name
  101. *
  102. * @api
  103. */
  104. public function getClientOriginalName()
  105. {
  106. return $this->originalName;
  107. }
  108. /**
  109. * Returns the original file extension
  110. *
  111. * It is extracted from the original file name that was uploaded.
  112. * Then is should not be considered as a safe value.
  113. *
  114. * @return string The extension
  115. */
  116. public function getClientOriginalExtension()
  117. {
  118. return pathinfo($this->originalName, PATHINFO_EXTENSION);
  119. }
  120. /**
  121. * Returns the file mime type.
  122. *
  123. * It is extracted from the request from which the file has been uploaded.
  124. * Then is should not be considered as a safe value.
  125. *
  126. * @return string|null The mime type
  127. *
  128. * @api
  129. */
  130. public function getClientMimeType()
  131. {
  132. return $this->mimeType;
  133. }
  134. /**
  135. * Returns the file size.
  136. *
  137. * It is extracted from the request from which the file has been uploaded.
  138. * Then is should not be considered as a safe value.
  139. *
  140. * @return integer|null The file size
  141. *
  142. * @api
  143. */
  144. public function getClientSize()
  145. {
  146. return $this->size;
  147. }
  148. /**
  149. * Returns the upload error.
  150. *
  151. * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
  152. * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
  153. *
  154. * @return integer The upload error
  155. *
  156. * @api
  157. */
  158. public function getError()
  159. {
  160. return $this->error;
  161. }
  162. /**
  163. * Returns whether the file was uploaded successfully.
  164. *
  165. * @return Boolean True if the file has been uploaded with HTTP and no error occurred.
  166. *
  167. * @api
  168. */
  169. public function isValid()
  170. {
  171. $isOk = $this->error === UPLOAD_ERR_OK;
  172. return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
  173. }
  174. /**
  175. * Moves the file to a new location.
  176. *
  177. * @param string $directory The destination folder
  178. * @param string $name The new file name
  179. *
  180. * @return File A File object representing the new file
  181. *
  182. * @throws FileException if, for any reason, the file could not have been moved
  183. *
  184. * @api
  185. */
  186. public function move($directory, $name = null)
  187. {
  188. if ($this->isValid()) {
  189. if ($this->test) {
  190. return parent::move($directory, $name);
  191. }
  192. $target = $this->getTargetFile($directory, $name);
  193. if (!@move_uploaded_file($this->getPathname(), $target)) {
  194. $error = error_get_last();
  195. throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
  196. }
  197. @chmod($target, 0666 & ~umask());
  198. return $target;
  199. }
  200. throw new FileException(sprintf('The file "%s" is not valid', $this->getPathname()));
  201. }
  202. /**
  203. * Returns the maximum size of an uploaded file as configured in php.ini
  204. *
  205. * @return int The maximum size of an uploaded file in bytes
  206. */
  207. public static function getMaxFilesize()
  208. {
  209. $max = strtolower(ini_get('upload_max_filesize'));
  210. if ('' === $max) {
  211. return PHP_INT_MAX;
  212. }
  213. if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $max, $match)) {
  214. $shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
  215. $bases = array('' => 10, '0' => 8, '0x' => 16);
  216. return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]];
  217. }
  218. return 0;
  219. }
  220. }