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