FileBag.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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;
  11. use Symfony\Component\HttpFoundation\File\UploadedFile;
  12. /**
  13. * FileBag is a container for HTTP headers.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. * @author Bulat Shakirzyanov <mallluhuct@gmail.com>
  17. *
  18. * @api
  19. */
  20. class FileBag extends ParameterBag
  21. {
  22. private static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
  23. /**
  24. * Constructor.
  25. *
  26. * @param array $parameters An array of HTTP files
  27. *
  28. * @api
  29. */
  30. public function __construct(array $parameters = array())
  31. {
  32. $this->replace($parameters);
  33. }
  34. /**
  35. * {@inheritdoc}
  36. *
  37. * @api
  38. */
  39. public function replace(array $files = array())
  40. {
  41. $this->parameters = array();
  42. $this->add($files);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. *
  47. * @api
  48. */
  49. public function set($key, $value)
  50. {
  51. if (!is_array($value) && !$value instanceof UploadedFile) {
  52. throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
  53. }
  54. parent::set($key, $this->convertFileInformation($value));
  55. }
  56. /**
  57. * {@inheritdoc}
  58. *
  59. * @api
  60. */
  61. public function add(array $files = array())
  62. {
  63. foreach ($files as $key => $file) {
  64. $this->set($key, $file);
  65. }
  66. }
  67. /**
  68. * Converts uploaded files to UploadedFile instances.
  69. *
  70. * @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
  71. *
  72. * @return array A (multi-dimensional) array of UploadedFile instances
  73. */
  74. protected function convertFileInformation($file)
  75. {
  76. if ($file instanceof UploadedFile) {
  77. return $file;
  78. }
  79. $file = $this->fixPhpFilesArray($file);
  80. if (is_array($file)) {
  81. $keys = array_keys($file);
  82. sort($keys);
  83. if ($keys == self::$fileKeys) {
  84. if (UPLOAD_ERR_NO_FILE == $file['error']) {
  85. $file = null;
  86. } else {
  87. $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
  88. }
  89. } else {
  90. $file = array_map(array($this, 'convertFileInformation'), $file);
  91. }
  92. }
  93. return $file;
  94. }
  95. /**
  96. * Fixes a malformed PHP $_FILES array.
  97. *
  98. * PHP has a bug that the format of the $_FILES array differs, depending on
  99. * whether the uploaded file fields had normal field names or array-like
  100. * field names ("normal" vs. "parent[child]").
  101. *
  102. * This method fixes the array to look like the "normal" $_FILES array.
  103. *
  104. * It's safe to pass an already converted array, in which case this method
  105. * just returns the original array unmodified.
  106. *
  107. * @param array $data
  108. *
  109. * @return array
  110. */
  111. protected function fixPhpFilesArray($data)
  112. {
  113. if (!is_array($data)) {
  114. return $data;
  115. }
  116. $keys = array_keys($data);
  117. sort($keys);
  118. if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
  119. return $data;
  120. }
  121. $files = $data;
  122. foreach (self::$fileKeys as $k) {
  123. unset($files[$k]);
  124. }
  125. foreach (array_keys($data['name']) as $key) {
  126. $files[$key] = $this->fixPhpFilesArray(array(
  127. 'error' => $data['error'][$key],
  128. 'name' => $data['name'][$key],
  129. 'type' => $data['type'][$key],
  130. 'tmp_name' => $data['tmp_name'][$key],
  131. 'size' => $data['size'][$key]
  132. ));
  133. }
  134. return $files;
  135. }
  136. }