From 4ea6ace9e040070c2584ce046c9e567e9e848c4b Mon Sep 17 00:00:00 2001 From: maks feltrin Date: Wed, 1 Jun 2016 01:51:34 +0200 Subject: [PATCH] moveTo: check targetDirectory and more - test for already moved file 1st (or 2nd) : testing a boolean can avoid heavier tests - unify string and empty string in a single test: the exception message suggests both requirements --- src/UploadedFile.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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;