UploadedFile.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=1);
  3. namespace GuzzleHttp\Psr7;
  4. use InvalidArgumentException;
  5. use Psr\Http\Message\StreamInterface;
  6. use Psr\Http\Message\UploadedFileInterface;
  7. use RuntimeException;
  8. class UploadedFile implements UploadedFileInterface
  9. {
  10. private const ERRORS = [
  11. UPLOAD_ERR_OK,
  12. UPLOAD_ERR_INI_SIZE,
  13. UPLOAD_ERR_FORM_SIZE,
  14. UPLOAD_ERR_PARTIAL,
  15. UPLOAD_ERR_NO_FILE,
  16. UPLOAD_ERR_NO_TMP_DIR,
  17. UPLOAD_ERR_CANT_WRITE,
  18. UPLOAD_ERR_EXTENSION,
  19. ];
  20. /**
  21. * @var string|null
  22. */
  23. private $clientFilename;
  24. /**
  25. * @var string|null
  26. */
  27. private $clientMediaType;
  28. /**
  29. * @var int
  30. */
  31. private $error;
  32. /**
  33. * @var string|null
  34. */
  35. private $file;
  36. /**
  37. * @var bool
  38. */
  39. private $moved = false;
  40. /**
  41. * @var int|null
  42. */
  43. private $size;
  44. /**
  45. * @var StreamInterface|null
  46. */
  47. private $stream;
  48. /**
  49. * @param StreamInterface|string|resource $streamOrFile
  50. */
  51. public function __construct(
  52. $streamOrFile,
  53. ?int $size,
  54. int $errorStatus,
  55. string $clientFilename = null,
  56. string $clientMediaType = null
  57. ) {
  58. $this->setError($errorStatus);
  59. $this->size = $size;
  60. $this->clientFilename = $clientFilename;
  61. $this->clientMediaType = $clientMediaType;
  62. if ($this->isOk()) {
  63. $this->setStreamOrFile($streamOrFile);
  64. }
  65. }
  66. /**
  67. * Depending on the value set file or stream variable
  68. *
  69. * @param StreamInterface|string|resource $streamOrFile
  70. *
  71. * @throws InvalidArgumentException
  72. */
  73. private function setStreamOrFile($streamOrFile): void
  74. {
  75. if (is_string($streamOrFile)) {
  76. $this->file = $streamOrFile;
  77. } elseif (is_resource($streamOrFile)) {
  78. $this->stream = new Stream($streamOrFile);
  79. } elseif ($streamOrFile instanceof StreamInterface) {
  80. $this->stream = $streamOrFile;
  81. } else {
  82. throw new InvalidArgumentException(
  83. 'Invalid stream or file provided for UploadedFile'
  84. );
  85. }
  86. }
  87. /**
  88. * @throws InvalidArgumentException
  89. */
  90. private function setError(int $error): void
  91. {
  92. if (false === in_array($error, UploadedFile::ERRORS, true)) {
  93. throw new InvalidArgumentException(
  94. 'Invalid error status for UploadedFile'
  95. );
  96. }
  97. $this->error = $error;
  98. }
  99. private static function isStringNotEmpty($param): bool
  100. {
  101. return is_string($param) && false === empty($param);
  102. }
  103. /**
  104. * Return true if there is no upload error
  105. */
  106. private function isOk(): bool
  107. {
  108. return $this->error === UPLOAD_ERR_OK;
  109. }
  110. public function isMoved(): bool
  111. {
  112. return $this->moved;
  113. }
  114. /**
  115. * @throws RuntimeException if is moved or not ok
  116. */
  117. private function validateActive(): void
  118. {
  119. if (false === $this->isOk()) {
  120. throw new RuntimeException('Cannot retrieve stream due to upload error');
  121. }
  122. if ($this->isMoved()) {
  123. throw new RuntimeException('Cannot retrieve stream after it has already been moved');
  124. }
  125. }
  126. public function getStream(): StreamInterface
  127. {
  128. $this->validateActive();
  129. if ($this->stream instanceof StreamInterface) {
  130. return $this->stream;
  131. }
  132. /** @var string $file */
  133. $file = $this->file;
  134. return new LazyOpenStream($file, 'r+');
  135. }
  136. public function moveTo($targetPath): void
  137. {
  138. $this->validateActive();
  139. if (false === self::isStringNotEmpty($targetPath)) {
  140. throw new InvalidArgumentException(
  141. 'Invalid path provided for move operation; must be a non-empty string'
  142. );
  143. }
  144. if ($this->file) {
  145. $this->moved = PHP_SAPI === 'cli'
  146. ? rename($this->file, $targetPath)
  147. : move_uploaded_file($this->file, $targetPath);
  148. } else {
  149. Utils::copyToStream(
  150. $this->getStream(),
  151. new LazyOpenStream($targetPath, 'w')
  152. );
  153. $this->moved = true;
  154. }
  155. if (false === $this->moved) {
  156. throw new RuntimeException(
  157. sprintf('Uploaded file could not be moved to %s', $targetPath)
  158. );
  159. }
  160. }
  161. public function getSize(): ?int
  162. {
  163. return $this->size;
  164. }
  165. public function getError(): int
  166. {
  167. return $this->error;
  168. }
  169. public function getClientFilename(): ?string
  170. {
  171. return $this->clientFilename;
  172. }
  173. public function getClientMediaType(): ?string
  174. {
  175. return $this->clientMediaType;
  176. }
  177. }