src/Valkyrja/HttpMessage/NativeUploadedFile.php
public function moveTo(string $targetPath): void
    {
        // If the error status is not OK
        if (UPLOAD_ERR_OK !== $this->errorStatus) {
            // Throw a runtime exception as there's been an uploaded file error
            throw new RuntimeException(
                'Cannot retrieve stream due to upload error'
            );
        }

        // If the file has already been moved
        if ($this->moved) {
            // Throw a runtime exception as subsequent moves are not allowed
            // in PSR-7
            throw new RuntimeException(
                'Cannot move file after it has already been moved'
            );
        }

        $targetDirectory = \dirname($targetPath);

        // If the target directory is not a directory
        // or the target directory is not writable
        if (! is_dir($targetDirectory) || ! is_writable($targetDirectory)) {
            // Throw a runtime exception
            throw new RuntimeException(
                sprintf(
                    'The target directory `%s` does not exists '
                    . 'or is not writable',
                    $targetDirectory
                )
            );
        }

        $sapi = PHP_SAPI;

        // If the PHP_SAPI value is empty
        // or there is no file
        // or the PHP_SAPI value is set to a CLI environment
        if (
            empty($sapi)
            || ! $this->file
            || 0 === strpos($sapi, 'cli')
        ) {
            // Non-SAPI environment, or no filename present
            $this->writeStream($targetPath);
        }
        // Otherwise try to use the move_uploaded_file function
        // and if the move_uploaded_file function call failed
        elseif (false === move_uploaded_file($this->file, $targetPath)) {
            // Throw a runtime exception
            throw new RuntimeException(
                'Error occurred while moving uploaded file'
            );
        }

        $this->moved = true;
    }
public function moveTo(string $targetPath): void
    {
        // If the error status is not OK
        if (UPLOAD_ERR_OK !== $this->errorStatus) {
            // Throw a runtime exception as there's been an uploaded file error
            throw new RuntimeException(
                'Cannot retrieve stream due to upload error'
            );
        }

        // If the file has already been moved
        if ($this->moved) {
            // Throw a runtime exception as subsequent moves are not allowed
            // in PSR-7
            throw new RuntimeException(
                'Cannot move file after it has already been moved'
            );
        }

        $targetDirectory = \dirname($targetPath);

        // If the target directory is not a directory
        // or the target directory is not writable
        if (! is_dir($targetDirectory) || ! is_writable($targetDirectory)) {
            // Throw a runtime exception
            throw new RuntimeException(
                sprintf(
                    'The target directory `%s` does not exists '
                    . 'or is not writable',
                    $targetDirectory
                )
            );
        }

        $sapi = PHP_SAPI;

        // If the PHP_SAPI value is empty
        // or there is no file
        // or the PHP_SAPI value is set to a CLI environment
        if (
            empty($sapi)
            || ! $this->file
            || 0 === strpos($sapi, 'cli')
        ) {
            // Non-SAPI environment, or no filename present
            $this->writeStream($targetPath);
        }
        // Otherwise try to use the move_uploaded_file function
        // and if the move_uploaded_file function call failed
        elseif (false === move_uploaded_file($this->file, $targetPath)) {
            // Throw a runtime exception
            throw new RuntimeException(
                'Error occurred while moving uploaded file'
            );
        }

        $this->moved = true;
    }
Move the uploaded file to a new location.

Use this method as an alternative to move_uploaded_file(). This method
is
guaranteed to work in both SAPI and non-SAPI environments.
Implementations must determine which environment they are in, and use
the appropriate method (move_uploaded_file(), rename(), or a stream
operation) to perform the operation.

$targetPath may be an absolute path, or a relative path. If it is a
relative path, resolution should be the same as used by PHP's rename()
function.

The original file or stream MUST be removed on completion.

If this method is called more than once, any subsequent calls MUST raise
an exception.

When used in an SAPI environment where $_FILES is populated, when
writing files via moveTo(), is_uploaded_file() and
move_uploaded_file() SHOULD be used to ensure permissions and upload
status are verified correctly.

If you wish to move to a stream, use getStream(), as SAPI operations
cannot guarantee writing to stream destinations.

@see http://php.net/is_uploaded_file
@see http://php.net/move_uploaded_file

@param string $targetPath Path to which to move the uploaded file.

@throws \InvalidArgumentException if the $targetPath specified is
invalid.
@throws \RuntimeException on any error during the move operation, or
on the second or subsequent call to the method.
@throws \Valkyrja\HttpMessage\Exceptions\InvalidStream

@return void