This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/HttpFoundation/FileBag.php

156 lines
3.9 KiB
PHP
Raw Normal View History

<?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 HTTP headers.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Bulat Shakirzyanov <mallluhuct@gmail.com>
2011-07-20 09:06:02 +01:00
*
* @api
*/
class FileBag extends ParameterBag
{
2012-07-09 13:50:58 +01:00
private static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
/**
* Constructor.
*
2011-04-23 16:05:44 +01:00
* @param array $parameters An array of HTTP files
2011-07-20 09:06:02 +01:00
*
* @api
*/
public function __construct(array $parameters = array())
{
$this->replace($parameters);
}
/**
2012-06-25 17:21:41 +01:00
* {@inheritdoc}
2011-07-20 09:06:02 +01:00
*
* @api
*/
public function replace(array $files = array())
{
$this->parameters = array();
$this->add($files);
}
/**
2012-06-25 17:21:41 +01:00
* {@inheritdoc}
2011-07-20 09:06:02 +01:00
*
* @api
*/
public function set($key, $value)
{
2012-06-25 17:21:41 +01:00
if (!is_array($value) && !$value instanceof UploadedFile) {
throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
}
2012-06-25 17:21:41 +01:00
parent::set($key, $this->convertFileInformation($value));
}
/**
2012-06-25 17:21:41 +01:00
* {@inheritdoc}
2011-07-20 09:06:02 +01:00
*
* @api
*/
public function add(array $files = array())
{
foreach ($files as $key => $file) {
$this->set($key, $file);
}
}
/**
* Converts uploaded files to UploadedFile instances.
*
2012-05-15 21:19:31 +01:00
* @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
*
* @return array A (multi-dimensional) array of UploadedFile instances
*/
protected function convertFileInformation($file)
{
if ($file instanceof UploadedFile) {
return $file;
}
$file = $this->fixPhpFilesArray($file);
if (is_array($file)) {
$keys = array_keys($file);
sort($keys);
if ($keys == self::$fileKeys) {
if (UPLOAD_ERR_NO_FILE == $file['error']) {
$file = null;
} else {
$file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
}
} else {
$file = array_map(array($this, 'convertFileInformation'), $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.
*
2012-05-18 18:41:48 +01:00
* @param array $data
2011-12-13 07:50:54 +00:00
*
* @return array
*/
protected function fixPhpFilesArray($data)
{
if (!is_array($data)) {
return $data;
}
$keys = array_keys($data);
sort($keys);
if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
return $data;
}
$files = $data;
foreach (self::$fileKeys as $k) {
unset($files[$k]);
}
foreach (array_keys($data['name']) as $key) {
$files[$key] = $this->fixPhpFilesArray(array(
'error' => $data['error'][$key],
2011-06-08 11:12:55 +01:00
'name' => $data['name'][$key],
2011-04-27 08:10:41 +01:00
'type' => $data['type'][$key],
'tmp_name' => $data['tmp_name'][$key],
'size' => $data['size'][$key]
));
}
return $files;
}
}