FileBag.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 const FILE_KEYS = ['error', 'full_path', 'name', 'size', 'tmp_name', 'type'];
  21. /**
  22. * @param array|UploadedFile[] $parameters An array of HTTP files
  23. */
  24. public function __construct(array $parameters = [])
  25. {
  26. $this->replace($parameters);
  27. }
  28. public function replace(array $files = []): void
  29. {
  30. $this->parameters = [];
  31. $this->add($files);
  32. }
  33. public function set(string $key, mixed $value): void
  34. {
  35. if (!\is_array($value) && !$value instanceof UploadedFile) {
  36. throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
  37. }
  38. parent::set($key, $this->convertFileInformation($value));
  39. }
  40. public function add(array $files = []): void
  41. {
  42. foreach ($files as $key => $file) {
  43. $this->set($key, $file);
  44. }
  45. }
  46. /**
  47. * Converts uploaded files to UploadedFile instances.
  48. *
  49. * @return UploadedFile[]|UploadedFile|null
  50. */
  51. protected function convertFileInformation(array|UploadedFile $file): array|UploadedFile|null
  52. {
  53. if ($file instanceof UploadedFile) {
  54. return $file;
  55. }
  56. $file = $this->fixPhpFilesArray($file);
  57. $keys = array_keys($file + ['full_path' => null]);
  58. sort($keys);
  59. if (self::FILE_KEYS === $keys) {
  60. if (\UPLOAD_ERR_NO_FILE === $file['error']) {
  61. $file = null;
  62. } else {
  63. $file = new UploadedFile($file['tmp_name'], $file['full_path'] ?? $file['name'], $file['type'], $file['error'], false);
  64. }
  65. } else {
  66. $file = array_map(fn ($v) => $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v, $file);
  67. if (array_is_list($file)) {
  68. $file = array_filter($file);
  69. }
  70. }
  71. return $file;
  72. }
  73. /**
  74. * Fixes a malformed PHP $_FILES array.
  75. *
  76. * PHP has a bug that the format of the $_FILES array differs, depending on
  77. * whether the uploaded file fields had normal field names or array-like
  78. * field names ("normal" vs. "parent[child]").
  79. *
  80. * This method fixes the array to look like the "normal" $_FILES array.
  81. *
  82. * It's safe to pass an already converted array, in which case this method
  83. * just returns the original array unmodified.
  84. */
  85. protected function fixPhpFilesArray(array $data): array
  86. {
  87. $keys = array_keys($data + ['full_path' => null]);
  88. sort($keys);
  89. if (self::FILE_KEYS !== $keys || !isset($data['name']) || !\is_array($data['name'])) {
  90. return $data;
  91. }
  92. $files = $data;
  93. foreach (self::FILE_KEYS as $k) {
  94. unset($files[$k]);
  95. }
  96. foreach ($data['name'] as $key => $name) {
  97. $files[$key] = $this->fixPhpFilesArray([
  98. 'error' => $data['error'][$key],
  99. 'name' => $name,
  100. 'type' => $data['type'][$key],
  101. 'tmp_name' => $data['tmp_name'][$key],
  102. 'size' => $data['size'][$key],
  103. ] + (isset($data['full_path'][$key]) ? [
  104. 'full_path' => $data['full_path'][$key],
  105. ] : []));
  106. }
  107. return $files;
  108. }
  109. }