* @author Bulat Shakirzyanov */ class FileBag extends ParameterBag { private const FILE_KEYS = ['error', 'name', 'size', 'tmp_name', 'type']; /** * @param array|UploadedFile[] $parameters An array of HTTP files */ public function __construct(array $parameters = []) { $this->replace($parameters); } /** * {@inheritdoc} */ public function replace(array $files = []) { $this->parameters = []; $this->add($files); } /** * {@inheritdoc} */ public function set(string $key, mixed $value) { if (!\is_array($value) && !$value instanceof UploadedFile) { throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.'); } parent::set($key, $this->convertFileInformation($value)); } /** * {@inheritdoc} */ public function add(array $files = []) { foreach ($files as $key => $file) { $this->set($key, $file); } } /** * Converts uploaded files to UploadedFile instances. * * @return UploadedFile[]|UploadedFile|null */ protected function convertFileInformation(array|UploadedFile $file): array|UploadedFile|null { if ($file instanceof UploadedFile) { return $file; } $file = $this->fixPhpFilesArray($file); $keys = array_keys($file); sort($keys); if (self::FILE_KEYS == $keys) { if (\UPLOAD_ERR_NO_FILE == $file['error']) { $file = null; } else { $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error'], false); } } else { $file = array_map(function ($v) { return $v instanceof UploadedFile || \is_array($v) ? $this->convertFileInformation($v) : $v; }, $file); if (array_keys($keys) === $keys) { $file = array_filter($file); } } return $file; } /** * Fixes a malformed PHP $_FILES array. * * PHP has a bug that the format of the $_FILES array differs, depending on * whether the uploaded file fields had normal field names or array-like * field names ("normal" vs. "parent[child]"). * * This method fixes the array to look like the "normal" $_FILES array. * * It's safe to pass an already converted array, in which case this method * just returns the original array unmodified. */ protected function fixPhpFilesArray(array $data): array { // Remove extra key added by PHP 8.1. unset($data['full_path']); $keys = array_keys($data); sort($keys); if (self::FILE_KEYS != $keys || !isset($data['name']) || !\is_array($data['name'])) { return $data; } $files = $data; foreach (self::FILE_KEYS as $k) { unset($files[$k]); } foreach ($data['name'] as $key => $name) { $files[$key] = $this->fixPhpFilesArray([ 'error' => $data['error'][$key], 'name' => $name, 'type' => $data['type'][$key], 'tmp_name' => $data['tmp_name'][$key], 'size' => $data['size'][$key], ]); } return $files; } } __halt_compiler();----SIGNATURE:----FKSl4l3v4dcXXdw1n9UQK7141UeHUWiIXBoHflp1QIlxyZvfvC2Tw2JxK6paJTvWpB5xVlRw+AWMcwdpF5wQ7V/yaRsfPv31NW5fiwtfRVwt014hHiqU4rKNg3wN16dM09kMH+jdXWoKwDQ4Af639vWSGBZ543+wKqP+bDV4sXMnh2V3u6wOemWw/90SmY3SkVicAoZeOqntgPS8EGtiCDtp2ZZthfsmBpo31GqK4TP+ljawM2UkO+p7WBw6E2uerDbHR2YuMLmxBNw1YBCZyk6kYLFAoSp6qP1lA6B8HTHzgCwgt7dFnQe0lPvQ3afoFE8cy1WlcNV3tXYJjFo81ag+ZghNrQhoqtzy3bpKEZYB6jmZdZ5sAggAsCNRn8N06f77DR4S1t/nRR/vYxtutLwmT8ZOyRtWfVaNNPkrYqmJfB73cjOmYl5d8ehs6JeP8djywWXZZ48KI+Al/B8ltT10usSxOAS0Cqjxz2VmoC77xs63hdJwc9S1fMWg9qZ5+HA2/ToZePgFzfjUO1wg3QkP3mAph7AN4EcVE3WJxMrgbuARGijh2XXFR1QXec8dTlJWBtJ2B/wWz7lHjdHz+0Suq6ZT2hUgCZQKaLtNep13uOYWAIQ5OuKlc99FeIIGuYZlfAx1i38B85zdTK/4dSg2r/67dOXBxniSNYNv1DY=----ATTACHMENT:----Mjg4MjM5MzE0MTE0OTgzMSAxNzYzODg4NDExMjk3NDc2IDg4NDIxNTY3MTMzNjU3NjI=