public function moveTo($targetPath)
{
if ($this->moved) {
throw new RuntimeException('Cannot move file; already moved!');
}
if ($this->error !== UPLOAD_ERR_OK) {
throw new RuntimeException('Cannot retrieve stream due to upload error');
}
if (!is_string($targetPath) || empty($targetPath)) {
throw new InvalidArgumentException('Invalid path provided for move operation; must be a non-empty string');
}
$targetDirectory = dirname($targetPath);
if (!is_dir($targetDirectory) || !is_writable($targetDirectory)) {
throw new RuntimeException(sprintf('The target directory `%s` does not exists or is not writable', $targetDirectory));
}
$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;
}