diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 5a93fb38..767724b8 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -142,24 +142,26 @@ public function getStream() */ public function moveTo($targetPath) { - if ($this->error !== UPLOAD_ERR_OK) { - throw new RuntimeException('Cannot retrieve stream due to upload error'); + if ($this->moved) { + throw new RuntimeException('Cannot move file; already moved!'); } - if (! is_string($targetPath)) { - throw new InvalidArgumentException( - 'Invalid path provided for move operation; must be a string' - ); + if ($this->error !== UPLOAD_ERR_OK) { + throw new RuntimeException('Cannot retrieve stream due to upload error'); } - if (empty($targetPath)) { + if (! is_string($targetPath) || 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!'); + $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;