diff --git a/src/UploadedFile.php b/src/UploadedFile.php index 93980f30..93e94195 100644 --- a/src/UploadedFile.php +++ b/src/UploadedFile.php @@ -187,6 +187,13 @@ public function moveTo($targetPath) : void case (empty($sapi) || 0 === strpos($sapi, 'cli') || 0 === strpos($sapi, 'phpdbg') || ! $this->file): // Non-SAPI environment, or no filename present $this->writeFile($targetPath); + + if ($this->stream instanceof StreamInterface) { + $this->stream->close(); + if (is_string($this->file) && file_exists($this->file)) { + unlink($this->file); + } + } break; default: // SAPI environment, with file present diff --git a/test/UploadedFileTest.php b/test/UploadedFileTest.php index f7518862..c741b838 100644 --- a/test/UploadedFileTest.php +++ b/test/UploadedFileTest.php @@ -32,11 +32,15 @@ class UploadedFileTest extends TestCase { + /** @var false|null|string */ + protected $orgFile; + protected $tmpFile; protected function setUp() : void { - $this->tmpfile = null; + $this->tmpFile = null; + $this->orgFile = null; } protected function tearDown() : void @@ -44,6 +48,10 @@ protected function tearDown() : void if (is_string($this->tmpFile) && file_exists($this->tmpFile)) { unlink($this->tmpFile); } + + if (is_string($this->orgFile) && file_exists($this->orgFile)) { + unlink($this->orgFile); + } } public function invalidStreams() @@ -142,17 +150,21 @@ public function testGetStreamReturnsStreamForFile() $this->assertSame($stream, $r->getValue($uploadStream)); } + /** + * @return void + */ public function testMovesFileToDesignatedPath() { + $originalContents = 'Foo bar!'; $stream = new Stream('php://temp', 'wb+'); - $stream->write('Foo bar!'); + $stream->write($originalContents); $upload = new UploadedFile($stream, 0, UPLOAD_ERR_OK); $this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'diac'); $upload->moveTo($to); $this->assertTrue(file_exists($to)); $contents = file_get_contents($to); - $this->assertSame($stream->__toString(), $contents); + $this->assertSame($originalContents, $contents); } public function invalidMovePaths() @@ -271,18 +283,22 @@ public function testGetStreamRaisesExceptionWhenErrorStatusPresent($status) /** * @group 82 + * @return void */ public function testMoveToCreatesStreamIfOnlyAFilenameWasProvided() { + $this->orgFile = tempnam(sys_get_temp_dir(), 'ORG'); $this->tmpFile = tempnam(sys_get_temp_dir(), 'DIA'); + file_put_contents($this->orgFile, 'Hello'); - $uploadedFile = new UploadedFile(__FILE__, 100, UPLOAD_ERR_OK, basename(__FILE__), 'text/plain'); + $original = file_get_contents($this->orgFile); + + $uploadedFile = new UploadedFile($this->orgFile, 100, UPLOAD_ERR_OK, basename($this->orgFile), 'text/plain'); $uploadedFile->moveTo($this->tmpFile); - $original = file_get_contents(__FILE__); - $test = file_get_contents($this->tmpFile); + $contents = file_get_contents($this->tmpFile); - $this->assertSame($original, $test); + $this->assertSame($original, $contents); } public function errorConstantsAndMessages() @@ -320,4 +336,19 @@ public function testMoveToRaisesExceptionWithAppropriateMessageWhenUploadErrorDe $this->expectExceptionMessage($message); $uploadedFile->moveTo('/tmp/foo'); } + + /** + * @return void + */ + public function testMoveToInCLIShouldRemoveOriginalFile() + { + $this->orgFile = tempnam(sys_get_temp_dir(), 'ORG'); + file_put_contents($this->orgFile, 'Hello'); + $upload = new UploadedFile($this->orgFile, 0, UPLOAD_ERR_OK); + + $this->tmpFile = $to = tempnam(sys_get_temp_dir(), 'diac'); + $upload->moveTo($to); + $this->assertFalse(file_exists($this->orgFile)); + $this->assertTrue(file_exists($to)); + } }