You are here

public function UploadedFile::moveTo in Zircon Profile 8

Same name in this branch
  1. 8 vendor/zendframework/zend-diactoros/src/UploadedFile.php \Zend\Diactoros\UploadedFile::moveTo()
  2. 8 vendor/symfony/psr-http-message-bridge/Tests/Fixtures/UploadedFile.php \Symfony\Bridge\PsrHttpMessage\Tests\Fixtures\UploadedFile::moveTo()
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/UploadedFile.php \Zend\Diactoros\UploadedFile::moveTo()

Parameters

string $targetPath Path to which to move the uploaded file.:

Throws

\RuntimeException if the upload was not successful.

\InvalidArgumentException if the $path specified is invalid.

\RuntimeException on any error during the move operation, or on the second or subsequent call to the method.

Overrides UploadedFileInterface::moveTo

See also

http://php.net/is_uploaded_file

http://php.net/move_uploaded_file

File

vendor/zendframework/zend-diactoros/src/UploadedFile.php, line 135

Class

UploadedFile

Namespace

Zend\Diactoros

Code

public function moveTo($targetPath) {
  if ($this->error !== UPLOAD_ERR_OK) {
    throw new RuntimeException('Cannot retrieve stream due to upload error');
  }
  if (!is_string($targetPath)) {
    throw new InvalidArgumentException('Invalid path provided for move operation; must be a string');
  }
  if (empty($targetPath)) {
    throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
  }
  if ($this->moved) {
    throw new RuntimeException('Cannot move file; already moved!');
  }
  $sapi = PHP_SAPI;
  switch (true) {
    case empty($sapi) || 0 === strpos($sapi, 'cli') || !$this->file:

      // Non-SAPI environment, or no filename present
      $this
        ->writeFile($targetPath);
      break;
    default:

      // SAPI environment, with file present
      if (false === move_uploaded_file($this->file, $targetPath)) {
        throw new RuntimeException('Error occurred while moving uploaded file');
      }
      break;
  }
  $this->moved = true;
}