FileBag.php 3.8 KB

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