[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/ -> UploadedFile.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros;
  12  
  13  use Psr\Http\Message\StreamInterface;
  14  use Psr\Http\Message\UploadedFileInterface;
  15  
  16  use function dirname;
  17  use function fclose;
  18  use function fopen;
  19  use function fwrite;
  20  use function is_dir;
  21  use function is_int;
  22  use function is_resource;
  23  use function is_string;
  24  use function is_writable;
  25  use function move_uploaded_file;
  26  use function sprintf;
  27  use function strpos;
  28  
  29  use const PHP_SAPI;
  30  use const UPLOAD_ERR_CANT_WRITE;
  31  use const UPLOAD_ERR_EXTENSION;
  32  use const UPLOAD_ERR_FORM_SIZE;
  33  use const UPLOAD_ERR_INI_SIZE;
  34  use const UPLOAD_ERR_NO_FILE;
  35  use const UPLOAD_ERR_NO_TMP_DIR;
  36  use const UPLOAD_ERR_OK;
  37  use const UPLOAD_ERR_PARTIAL;
  38  
  39  class UploadedFile implements UploadedFileInterface
  40  {
  41      const ERROR_MESSAGES = [
  42          UPLOAD_ERR_OK         => 'There is no error, the file uploaded with success',
  43          UPLOAD_ERR_INI_SIZE   => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
  44          UPLOAD_ERR_FORM_SIZE  => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was '
  45              . 'specified in the HTML form',
  46          UPLOAD_ERR_PARTIAL    => 'The uploaded file was only partially uploaded',
  47          UPLOAD_ERR_NO_FILE    => 'No file was uploaded',
  48          UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder',
  49          UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk',
  50          UPLOAD_ERR_EXTENSION  => 'A PHP extension stopped the file upload.',
  51      ];
  52  
  53      /**
  54       * @var string|null
  55       */
  56      private $clientFilename;
  57  
  58      /**
  59       * @var string|null
  60       */
  61      private $clientMediaType;
  62  
  63      /**
  64       * @var int
  65       */
  66      private $error;
  67  
  68      /**
  69       * @var null|string
  70       */
  71      private $file;
  72  
  73      /**
  74       * @var bool
  75       */
  76      private $moved = false;
  77  
  78      /**
  79       * @var int
  80       */
  81      private $size;
  82  
  83      /**
  84       * @var null|StreamInterface
  85       */
  86      private $stream;
  87  
  88      /**
  89       * @param string|resource $streamOrFile
  90       * @param int $size
  91       * @param int $errorStatus
  92       * @param string|null $clientFilename
  93       * @param string|null $clientMediaType
  94       * @throws Exception\InvalidArgumentException
  95       */
  96      public function __construct(
  97          $streamOrFile,
  98          int $size,
  99          int $errorStatus,
 100          string $clientFilename = null,
 101          string $clientMediaType = null
 102      ) {
 103          if ($errorStatus === UPLOAD_ERR_OK) {
 104              if (is_string($streamOrFile)) {
 105                  $this->file = $streamOrFile;
 106              }
 107              if (is_resource($streamOrFile)) {
 108                  $this->stream = new Stream($streamOrFile);
 109              }
 110  
 111              if (! $this->file && ! $this->stream) {
 112                  if (! $streamOrFile instanceof StreamInterface) {
 113                      throw new Exception\InvalidArgumentException('Invalid stream or file provided for UploadedFile');
 114                  }
 115                  $this->stream = $streamOrFile;
 116              }
 117          }
 118  
 119          $this->size = $size;
 120  
 121          if (0 > $errorStatus || 8 < $errorStatus) {
 122              throw new Exception\InvalidArgumentException(
 123                  'Invalid error status for UploadedFile; must be an UPLOAD_ERR_* constant'
 124              );
 125          }
 126          $this->error = $errorStatus;
 127  
 128          $this->clientFilename = $clientFilename;
 129          $this->clientMediaType = $clientMediaType;
 130      }
 131  
 132      /**
 133       * {@inheritdoc}
 134       * @throws Exception\UploadedFileAlreadyMovedException if the upload was
 135       *     not successful.
 136       */
 137      public function getStream() : StreamInterface
 138      {
 139          if ($this->error !== UPLOAD_ERR_OK) {
 140              throw Exception\UploadedFileErrorException::dueToStreamUploadError(
 141                  self::ERROR_MESSAGES[$this->error]
 142              );
 143          }
 144  
 145          if ($this->moved) {
 146              throw new Exception\UploadedFileAlreadyMovedException();
 147          }
 148  
 149          if ($this->stream instanceof StreamInterface) {
 150              return $this->stream;
 151          }
 152  
 153          $this->stream = new Stream($this->file);
 154          return $this->stream;
 155      }
 156  
 157      /**
 158       * {@inheritdoc}
 159       *
 160       * @see http://php.net/is_uploaded_file
 161       * @see http://php.net/move_uploaded_file
 162       * @param string $targetPath Path to which to move the uploaded file.
 163       * @throws Exception\UploadedFileErrorException if the upload was not successful.
 164       * @throws Exception\InvalidArgumentException if the $path specified is invalid.
 165       * @throws Exception\UploadedFileErrorException on any error during the
 166       *     move operation, or on the second or subsequent call to the method.
 167       */
 168      public function moveTo($targetPath) : void
 169      {
 170          if ($this->moved) {
 171              throw new Exception\UploadedFileAlreadyMovedException('Cannot move file; already moved!');
 172          }
 173  
 174          if ($this->error !== UPLOAD_ERR_OK) {
 175              throw Exception\UploadedFileErrorException::dueToStreamUploadError(
 176                  self::ERROR_MESSAGES[$this->error]
 177              );
 178          }
 179  
 180          if (! is_string($targetPath) || empty($targetPath)) {
 181              throw new Exception\InvalidArgumentException(
 182                  'Invalid path provided for move operation; must be a non-empty string'
 183              );
 184          }
 185  
 186          $targetDirectory = dirname($targetPath);
 187          if (! is_dir($targetDirectory) || ! is_writable($targetDirectory)) {
 188              throw Exception\UploadedFileErrorException::dueToUnwritableTarget($targetDirectory);
 189          }
 190  
 191          $sapi = PHP_SAPI;
 192          switch (true) {
 193              case (empty($sapi) || 0 === strpos($sapi, 'cli') || 0 === strpos($sapi, 'phpdbg') || ! $this->file):
 194                  // Non-SAPI environment, or no filename present
 195                  $this->writeFile($targetPath);
 196                  break;
 197              default:
 198                  // SAPI environment, with file present
 199                  if (false === move_uploaded_file($this->file, $targetPath)) {
 200                      throw Exception\UploadedFileErrorException::forUnmovableFile();
 201                  }
 202                  break;
 203          }
 204  
 205          $this->moved = true;
 206      }
 207  
 208      /**
 209       * {@inheritdoc}
 210       *
 211       * @return int|null The file size in bytes or null if unknown.
 212       */
 213      public function getSize() : ?int
 214      {
 215          return $this->size;
 216      }
 217  
 218      /**
 219       * {@inheritdoc}
 220       *
 221       * @see http://php.net/manual/en/features.file-upload.errors.php
 222       * @return int One of PHP's UPLOAD_ERR_XXX constants.
 223       */
 224      public function getError() : int
 225      {
 226          return $this->error;
 227      }
 228  
 229      /**
 230       * {@inheritdoc}
 231       *
 232       * @return string|null The filename sent by the client or null if none
 233       *     was provided.
 234       */
 235      public function getClientFilename() : ?string
 236      {
 237          return $this->clientFilename;
 238      }
 239  
 240      /**
 241       * {@inheritdoc}
 242       */
 243      public function getClientMediaType() : ?string
 244      {
 245          return $this->clientMediaType;
 246      }
 247  
 248      /**
 249       * Write internal stream to given path
 250       *
 251       * @param string $path
 252       */
 253      private function writeFile(string $path) : void
 254      {
 255          $handle = fopen($path, 'wb+');
 256          if (false === $handle) {
 257              throw Exception\UploadedFileErrorException::dueToUnwritablePath();
 258          }
 259  
 260          $stream = $this->getStream();
 261          $stream->rewind();
 262          while (! $stream->eof()) {
 263              fwrite($handle, $stream->read(4096));
 264          }
 265  
 266          fclose($handle);
 267      }
 268  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer