FileBag.php 4.1 KB

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