FileBag.php 3.9 KB

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