[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/php-ai-client/third-party/Nyholm/Psr7/ -> UploadedFile.php (source)

   1  <?php
   2  
   3  declare (strict_types=1);
   4  namespace WordPress\AiClientDependencies\Nyholm\Psr7;
   5  
   6  use WordPress\AiClientDependencies\Psr\Http\Message\StreamInterface;
   7  use WordPress\AiClientDependencies\Psr\Http\Message\UploadedFileInterface;
   8  /**
   9   * @author Michael Dowling and contributors to guzzlehttp/psr7
  10   * @author Tobias Nyholm <tobias.nyholm@gmail.com>
  11   * @author Martijn van der Ven <martijn@vanderven.se>
  12   *
  13   * @final This class should never be extended. See https://github.com/Nyholm/psr7/blob/master/doc/final.md
  14   */
  15  class UploadedFile implements UploadedFileInterface
  16  {
  17      /** @var array */
  18      private const ERRORS = [\UPLOAD_ERR_OK => 1, \UPLOAD_ERR_INI_SIZE => 1, \UPLOAD_ERR_FORM_SIZE => 1, \UPLOAD_ERR_PARTIAL => 1, \UPLOAD_ERR_NO_FILE => 1, \UPLOAD_ERR_NO_TMP_DIR => 1, \UPLOAD_ERR_CANT_WRITE => 1, \UPLOAD_ERR_EXTENSION => 1];
  19      /** @var string */
  20      private $clientFilename;
  21      /** @var string */
  22      private $clientMediaType;
  23      /** @var int */
  24      private $error;
  25      /** @var string|null */
  26      private $file;
  27      /** @var bool */
  28      private $moved = \false;
  29      /** @var int */
  30      private $size;
  31      /** @var StreamInterface|null */
  32      private $stream;
  33      /**
  34       * @param StreamInterface|string|resource $streamOrFile
  35       * @param int $size
  36       * @param int $errorStatus
  37       * @param string|null $clientFilename
  38       * @param string|null $clientMediaType
  39       */
  40      public function __construct($streamOrFile, $size, $errorStatus, $clientFilename = null, $clientMediaType = null)
  41      {
  42          if (\false === \is_int($errorStatus) || !isset(self::ERRORS[$errorStatus])) {
  43              throw new \InvalidArgumentException('Upload file error status must be an integer value and one of the "UPLOAD_ERR_*" constants');
  44          }
  45          if (\false === \is_int($size)) {
  46              throw new \InvalidArgumentException('Upload file size must be an integer');
  47          }
  48          if (null !== $clientFilename && !\is_string($clientFilename)) {
  49              throw new \InvalidArgumentException('Upload file client filename must be a string or null');
  50          }
  51          if (null !== $clientMediaType && !\is_string($clientMediaType)) {
  52              throw new \InvalidArgumentException('Upload file client media type must be a string or null');
  53          }
  54          $this->error = $errorStatus;
  55          $this->size = $size;
  56          $this->clientFilename = $clientFilename;
  57          $this->clientMediaType = $clientMediaType;
  58          if (\UPLOAD_ERR_OK === $this->error) {
  59              // Depending on the value set file or stream variable.
  60              if (\is_string($streamOrFile) && '' !== $streamOrFile) {
  61                  $this->file = $streamOrFile;
  62              } elseif (\is_resource($streamOrFile)) {
  63                  $this->stream = Stream::create($streamOrFile);
  64              } elseif ($streamOrFile instanceof StreamInterface) {
  65                  $this->stream = $streamOrFile;
  66              } else {
  67                  throw new \InvalidArgumentException('Invalid stream or file provided for UploadedFile');
  68              }
  69          }
  70      }
  71      /**
  72       * @throws \RuntimeException if is moved or not ok
  73       */
  74      private function validateActive(): void
  75      {
  76          if (\UPLOAD_ERR_OK !== $this->error) {
  77              throw new \RuntimeException('Cannot retrieve stream due to upload error');
  78          }
  79          if ($this->moved) {
  80              throw new \RuntimeException('Cannot retrieve stream after it has already been moved');
  81          }
  82      }
  83      public function getStream(): StreamInterface
  84      {
  85          $this->validateActive();
  86          if ($this->stream instanceof StreamInterface) {
  87              return $this->stream;
  88          }
  89          if (\false === $resource = @\fopen($this->file, 'r')) {
  90              throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $this->file, \error_get_last()['message'] ?? ''));
  91          }
  92          return Stream::create($resource);
  93      }
  94      public function moveTo($targetPath): void
  95      {
  96          $this->validateActive();
  97          if (!\is_string($targetPath) || '' === $targetPath) {
  98              throw new \InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
  99          }
 100          if (null !== $this->file) {
 101              $this->moved = 'cli' === \PHP_SAPI ? @\rename($this->file, $targetPath) : @\move_uploaded_file($this->file, $targetPath);
 102              if (\false === $this->moved) {
 103                  throw new \RuntimeException(\sprintf('Uploaded file could not be moved to "%s": %s', $targetPath, \error_get_last()['message'] ?? ''));
 104              }
 105          } else {
 106              $stream = $this->getStream();
 107              if ($stream->isSeekable()) {
 108                  $stream->rewind();
 109              }
 110              if (\false === $resource = @\fopen($targetPath, 'w')) {
 111                  throw new \RuntimeException(\sprintf('The file "%s" cannot be opened: %s', $targetPath, \error_get_last()['message'] ?? ''));
 112              }
 113              $dest = Stream::create($resource);
 114              while (!$stream->eof()) {
 115                  if (!$dest->write($stream->read(1048576))) {
 116                      break;
 117                  }
 118              }
 119              $this->moved = \true;
 120          }
 121      }
 122      public function getSize(): int
 123      {
 124          return $this->size;
 125      }
 126      public function getError(): int
 127      {
 128          return $this->error;
 129      }
 130      public function getClientFilename(): ?string
 131      {
 132          return $this->clientFilename;
 133      }
 134      public function getClientMediaType(): ?string
 135      {
 136          return $this->clientMediaType;
 137      }
 138  }


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref