Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

UploadedFile.php

Zuletzt modifiziert: 09.10.2024, 12:56 - Dateigröße: 8.91 KiB


 001  <?php
 002   
 003  /*
 004   * This file is part of the Symfony package.
 005   *
 006   * (c) Fabien Potencier <fabien@symfony.com>
 007   *
 008   * For the full copyright and license information, please view the LICENSE
 009   * file that was distributed with this source code.
 010   */
 011   
 012  namespace Symfony\Component\HttpFoundation\File;
 013   
 014  use Symfony\Component\HttpFoundation\File\Exception\FileException;
 015  use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
 016  use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;
 017   
 018  /**
 019   * A file uploaded through a form.
 020   *
 021   * @author Bernhard Schussek <bschussek@gmail.com>
 022   * @author Florian Eckerstorfer <florian@eckerstorfer.org>
 023   * @author Fabien Potencier <fabien@symfony.com>
 024   */
 025  class UploadedFile extends File
 026  {
 027      /**
 028       * Whether the test mode is activated.
 029       *
 030       * Local files are used in test mode hence the code should not enforce HTTP uploads.
 031       *
 032       * @var bool
 033       */
 034      private $test = false;
 035   
 036      /**
 037       * The original name of the uploaded file.
 038       *
 039       * @var string
 040       */
 041      private $originalName;
 042   
 043      /**
 044       * The mime type provided by the uploader.
 045       *
 046       * @var string
 047       */
 048      private $mimeType;
 049   
 050      /**
 051       * The file size provided by the uploader.
 052       *
 053       * @var int|null
 054       */
 055      private $size;
 056   
 057      /**
 058       * The UPLOAD_ERR_XXX constant provided by the uploader.
 059       *
 060       * @var int
 061       */
 062      private $error;
 063   
 064      /**
 065       * Accepts the information of the uploaded file as provided by the PHP global $_FILES.
 066       *
 067       * The file object is only created when the uploaded file is valid (i.e. when the
 068       * isValid() method returns true). Otherwise the only methods that could be called
 069       * on an UploadedFile instance are:
 070       *
 071       *   * getClientOriginalName,
 072       *   * getClientMimeType,
 073       *   * isValid,
 074       *   * getError.
 075       *
 076       * Calling any other method on an non-valid instance will cause an unpredictable result.
 077       *
 078       * @param string      $path         The full temporary path to the file
 079       * @param string      $originalName The original file name
 080       * @param string|null $mimeType     The type of the file as provided by PHP; null defaults to application/octet-stream
 081       * @param int|null    $size         The file size
 082       * @param int|null    $error        The error constant of the upload (one of PHP's UPLOAD_ERR_XXX constants); null defaults to UPLOAD_ERR_OK
 083       * @param bool        $test         Whether the test mode is active
 084       *
 085       * @throws FileException         If file_uploads is disabled
 086       * @throws FileNotFoundException If the file does not exist
 087       */
 088      public function __construct($path, $originalName, $mimeType = null, $size = null, $error = null, $test = false)
 089      {
 090          $this->originalName = $this->getName($originalName);
 091          $this->mimeType = $mimeType ?: 'application/octet-stream';
 092          $this->size = $size;
 093          $this->error = $error ?: UPLOAD_ERR_OK;
 094          $this->test = (bool) $test;
 095   
 096          parent::__construct($path, UPLOAD_ERR_OK === $this->error);
 097      }
 098   
 099      /**
 100       * Returns the original file name.
 101       *
 102       * It is extracted from the request from which the file has been uploaded.
 103       * Then it should not be considered as a safe value.
 104       *
 105       * @return string|null The original name
 106       */
 107      public function getClientOriginalName()
 108      {
 109          return $this->originalName;
 110      }
 111   
 112      /**
 113       * Returns the original file extension.
 114       *
 115       * It is extracted from the original file name that was uploaded.
 116       * Then it should not be considered as a safe value.
 117       *
 118       * @return string The extension
 119       */
 120      public function getClientOriginalExtension()
 121      {
 122          return pathinfo($this->originalName, PATHINFO_EXTENSION);
 123      }
 124   
 125      /**
 126       * Returns the file mime type.
 127       *
 128       * The client mime type is extracted from the request from which the file
 129       * was uploaded, so it should not be considered as a safe value.
 130       *
 131       * For a trusted mime type, use getMimeType() instead (which guesses the mime
 132       * type based on the file content).
 133       *
 134       * @return string|null The mime type
 135       *
 136       * @see getMimeType()
 137       */
 138      public function getClientMimeType()
 139      {
 140          return $this->mimeType;
 141      }
 142   
 143      /**
 144       * Returns the extension based on the client mime type.
 145       *
 146       * If the mime type is unknown, returns null.
 147       *
 148       * This method uses the mime type as guessed by getClientMimeType()
 149       * to guess the file extension. As such, the extension returned
 150       * by this method cannot be trusted.
 151       *
 152       * For a trusted extension, use guessExtension() instead (which guesses
 153       * the extension based on the guessed mime type for the file).
 154       *
 155       * @return string|null The guessed extension or null if it cannot be guessed
 156       *
 157       * @see guessExtension()
 158       * @see getClientMimeType()
 159       */
 160      public function guessClientExtension()
 161      {
 162          $type = $this->getClientMimeType();
 163          $guesser = ExtensionGuesser::getInstance();
 164   
 165          return $guesser->guess($type);
 166      }
 167   
 168      /**
 169       * Returns the file size.
 170       *
 171       * It is extracted from the request from which the file has been uploaded.
 172       * Then it should not be considered as a safe value.
 173       *
 174       * @return int|null The file size
 175       */
 176      public function getClientSize()
 177      {
 178          return $this->size;
 179      }
 180   
 181      /**
 182       * Returns the upload error.
 183       *
 184       * If the upload was successful, the constant UPLOAD_ERR_OK is returned.
 185       * Otherwise one of the other UPLOAD_ERR_XXX constants is returned.
 186       *
 187       * @return int The upload error
 188       */
 189      public function getError()
 190      {
 191          return $this->error;
 192      }
 193   
 194      /**
 195       * Returns whether the file was uploaded successfully.
 196       *
 197       * @return bool True if the file has been uploaded with HTTP and no error occurred
 198       */
 199      public function isValid()
 200      {
 201          $isOk = $this->error === UPLOAD_ERR_OK;
 202   
 203          return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname());
 204      }
 205   
 206      /**
 207       * Moves the file to a new location.
 208       *
 209       * @param string $directory The destination folder
 210       * @param string $name      The new file name
 211       *
 212       * @return File A File object representing the new file
 213       *
 214       * @throws FileException if, for any reason, the file could not have been moved
 215       */
 216      public function move($directory, $name = null)
 217      {
 218          if ($this->isValid()) {
 219              if ($this->test) {
 220                  return parent::move($directory, $name);
 221              }
 222   
 223              $target = $this->getTargetFile($directory, $name);
 224   
 225              if (!@move_uploaded_file($this->getPathname(), $target)) {
 226                  $error = error_get_last();
 227                  throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
 228              }
 229   
 230              @chmod($target, 0666 & ~umask());
 231   
 232              return $target;
 233          }
 234   
 235          throw new FileException($this->getErrorMessage());
 236      }
 237   
 238      /**
 239       * Returns the maximum size of an uploaded file as configured in php.ini.
 240       *
 241       * @return int The maximum size of an uploaded file in bytes
 242       */
 243      public static function getMaxFilesize()
 244      {
 245          $iniMax = strtolower(ini_get('upload_max_filesize'));
 246   
 247          if ('' === $iniMax) {
 248              return PHP_INT_MAX;
 249          }
 250   
 251          $max = ltrim($iniMax, '+');
 252          if (0 === strpos($max, '0x')) {
 253              $max = intval($max, 16);
 254          } elseif (0 === strpos($max, '0')) {
 255              $max = intval($max, 8);
 256          } else {
 257              $max = (int) $max;
 258          }
 259   
 260          switch (substr($iniMax, -1)) {
 261              case 't': $max *= 1024;
 262              case 'g': $max *= 1024;
 263              case 'm': $max *= 1024;
 264              case 'k': $max *= 1024;
 265          }
 266   
 267          return $max;
 268      }
 269   
 270      /**
 271       * Returns an informative upload error message.
 272       *
 273       * @return string The error message regarding the specified error code
 274       */
 275      public function getErrorMessage()
 276      {
 277          static $errors = array(
 278              UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
 279              UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
 280              UPLOAD_ERR_PARTIAL => 'The file "%s" was only partially uploaded.',
 281              UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
 282              UPLOAD_ERR_CANT_WRITE => 'The file "%s" could not be written on disk.',
 283              UPLOAD_ERR_NO_TMP_DIR => 'File could not be uploaded: missing temporary directory.',
 284              UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
 285          );
 286   
 287          $errorCode = $this->error;
 288          $maxFilesize = $errorCode === UPLOAD_ERR_INI_SIZE ? self::getMaxFilesize() / 1024 : 0;
 289          $message = isset($errors[$errorCode]) ? $errors[$errorCode] : 'The file "%s" was not uploaded due to an unknown error.';
 290   
 291          return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
 292      }
 293  }
 294