You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
zhishifufei_php/vendor/symfony/http-foundation/FileBag.php

143 lines
3.8 KiB

10 months ago
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* FileBag is a container for uploaded files.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
*/
class FileBag extends ParameterBag
{
8 months ago
private static $fileKeys = ['error', 'name', 'size', 'tmp_name', 'type'];
10 months ago
/**
* @param array $parameters An array of HTTP files
*/
8 months ago
public function __construct(array $parameters = [])
10 months ago
{
$this->replace($parameters);
}
/**
* {@inheritdoc}
*/
8 months ago
public function replace(array $files = [])
10 months ago
{
8 months ago
$this->parameters = [];
10 months ago
$this->add($files);
}
/**
* {@inheritdoc}
*/
public function set($key, $value)
{
8 months ago
if (!\is_array($value) && !$value instanceof UploadedFile) {
10 months ago
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
}
parent::set($key, $this->convertFileInformation($value));
}
/**
* {@inheritdoc}
*/
8 months ago
public function add(array $files = [])
10 months ago
{
foreach ($files as $key => $file) {
$this->set($key, $file);
}
}
/**
* Converts uploaded files to UploadedFile instances.
*
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
*
* @return UploadedFile[]|UploadedFile|null A (multi-dimensional) array of UploadedFile instances
*/
protected function convertFileInformation($file)
{
if ($file instanceof UploadedFile) {
return $file;
}
8 months ago
if (\is_array($file)) {
$file = $this->fixPhpFilesArray($file);
10 months ago
$keys = array_keys($file);
sort($keys);
if ($keys == self::$fileKeys) {
8 months ago
if (\UPLOAD_ERR_NO_FILE == $file['error']) {
10 months ago
$file = null;
} else {
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
}
} else {
8 months ago
$file = array_map([$this, 'convertFileInformation'], $file);
10 months ago
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.
*
8 months ago
* @param array $data
*
10 months ago
* @return array
*/
protected function fixPhpFilesArray($data)
{
$keys = array_keys($data);
sort($keys);
8 months ago
if (self::$fileKeys != $keys || !isset($data['name']) || !\is_array($data['name'])) {
10 months ago
return $data;
}
$files = $data;
foreach (self::$fileKeys as $k) {
unset($files[$k]);
}
foreach ($data['name'] as $key => $name) {
8 months ago
$files[$key] = $this->fixPhpFilesArray([
10 months ago
'error' => $data['error'][$key],
'name' => $name,
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key],
8 months ago
]);
10 months ago
}
return $files;
}
}