[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ -> FileBag.php (source)

   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 static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type');
  25  
  26      /**
  27       * Constructor.
  28       *
  29       * @param array $parameters An array of HTTP files
  30       */
  31      public function __construct(array $parameters = array())
  32      {
  33          $this->replace($parameters);
  34      }
  35  
  36      /**
  37       * {@inheritdoc}
  38       */
  39      public function replace(array $files = array())
  40      {
  41          $this->parameters = array();
  42          $this->add($files);
  43      }
  44  
  45      /**
  46       * {@inheritdoc}
  47       */
  48      public function set($key, $value)
  49      {
  50          if (!is_array($value) && !$value instanceof UploadedFile) {
  51              throw new \InvalidArgumentException('An uploaded file must be an array or an instance of UploadedFile.');
  52          }
  53  
  54          parent::set($key, $this->convertFileInformation($value));
  55      }
  56  
  57      /**
  58       * {@inheritdoc}
  59       */
  60      public function add(array $files = array())
  61      {
  62          foreach ($files as $key => $file) {
  63              $this->set($key, $file);
  64          }
  65      }
  66  
  67      /**
  68       * Converts uploaded files to UploadedFile instances.
  69       *
  70       * @param array|UploadedFile $file A (multi-dimensional) array of uploaded file information
  71       *
  72       * @return array A (multi-dimensional) array of UploadedFile instances
  73       */
  74      protected function convertFileInformation($file)
  75      {
  76          if ($file instanceof UploadedFile) {
  77              return $file;
  78          }
  79  
  80          $file = $this->fixPhpFilesArray($file);
  81          if (is_array($file)) {
  82              $keys = array_keys($file);
  83              sort($keys);
  84  
  85              if ($keys == self::$fileKeys) {
  86                  if (UPLOAD_ERR_NO_FILE == $file['error']) {
  87                      $file = null;
  88                  } else {
  89                      $file = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error']);
  90                  }
  91              } else {
  92                  $file = array_map(array($this, 'convertFileInformation'), $file);
  93              }
  94          }
  95  
  96          return $file;
  97      }
  98  
  99      /**
 100       * Fixes a malformed PHP $_FILES array.
 101       *
 102       * PHP has a bug that the format of the $_FILES array differs, depending on
 103       * whether the uploaded file fields had normal field names or array-like
 104       * field names ("normal" vs. "parent[child]").
 105       *
 106       * This method fixes the array to look like the "normal" $_FILES array.
 107       *
 108       * It's safe to pass an already converted array, in which case this method
 109       * just returns the original array unmodified.
 110       *
 111       * @param array $data
 112       *
 113       * @return array
 114       */
 115      protected function fixPhpFilesArray($data)
 116      {
 117          if (!is_array($data)) {
 118              return $data;
 119          }
 120  
 121          $keys = array_keys($data);
 122          sort($keys);
 123  
 124          if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) {
 125              return $data;
 126          }
 127  
 128          $files = $data;
 129          foreach (self::$fileKeys as $k) {
 130              unset($files[$k]);
 131          }
 132  
 133          foreach ($data['name'] as $key => $name) {
 134              $files[$key] = $this->fixPhpFilesArray(array(
 135                  'error' => $data['error'][$key],
 136                  'name' => $name,
 137                  'type' => $data['type'][$key],
 138                  'tmp_name' => $data['tmp_name'][$key],
 139                  'size' => $data['size'][$key],
 140              ));
 141          }
 142  
 143          return $files;
 144      }
 145  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1