UploadedFile.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 <bernhard.schussek@symfony.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 = basename($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 file mime type.
  110. *
  111. * It is extracted from the request from which the file has been uploaded.
  112. * Then is should not be considered as a safe value.
  113. *
  114. * @return string|null The mime type
  115. *
  116. * @api
  117. */
  118. public function getClientMimeType()
  119. {
  120. return $this->mimeType;
  121. }
  122. /**
  123. * Returns the file size.
  124. *
  125. * It is extracted from the request from which the file has been uploaded.
  126. * Then is should not be considered as a safe value.
  127. *
  128. * @return integer|null The file size
  129. *
  130. * @api
  131. */
  132. public function getClientSize()
  133. {
  134. return $this->size;
  135. }
  136. /**
  137. * Returns the upload error.
  138. *
  139. * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
  140. * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
  141. *
  142. * @return integer The upload error
  143. *
  144. * @api
  145. */
  146. public function getError()
  147. {
  148. return $this->error;
  149. }
  150. /**
  151. * Returns whether the file was uploaded successfully.
  152. *
  153. * @return Boolean True if no error occurred during uploading
  154. *
  155. * @api
  156. */
  157. public function isValid()
  158. {
  159. return $this->error === UPLOAD_ERR_OK;
  160. }
  161. /**
  162. * Moves the file to a new location.
  163. *
  164. * @param string $directory The destination folder
  165. * @param string $name The new file name
  166. *
  167. * @return File A File object representing the new file
  168. *
  169. * @throws FileException if the file has not been uploaded via Http
  170. *
  171. * @api
  172. */
  173. public function move($directory, $name = null)
  174. {
  175. if ($this->isValid() && ($this->test || is_uploaded_file($this->getPathname()))) {
  176. return parent::move($directory, $name);
  177. }
  178. throw new FileException(sprintf('The file "%s" has not been uploaded via Http', $this->getPathname()));
  179. }
  180. /**
  181. * Returns the maximum size of an uploaded file as configured in php.ini
  182. *
  183. * @return type The maximum size of an uploaded file in bytes
  184. */
  185. static public function getMaxFilesize()
  186. {
  187. $max = trim(ini_get('upload_max_filesize'));
  188. if ('' === $max) {
  189. return PHP_INT_MAX;
  190. }
  191. switch (strtolower(substr($max, -1))) {
  192. case 'g':
  193. $max *= 1024;
  194. case 'm':
  195. $max *= 1024;
  196. case 'k':
  197. $max *= 1024;
  198. }
  199. return (integer) $max;
  200. }
  201. }