Skip to content

Commit

Permalink
fixed UploadedFile::moveTo doesn't work by cli. Original file leave i…
Browse files Browse the repository at this point in the history
…t as is.

Signed-off-by: katsuren <katsuren.houken@gmail.com>
  • Loading branch information
k2rn authored and Ocramius committed Jul 6, 2022
1 parent 78846cb commit 5773e76
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
7 changes: 7 additions & 0 deletions src/UploadedFile.php
Expand Up @@ -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
Expand Down
45 changes: 38 additions & 7 deletions test/UploadedFileTest.php
Expand Up @@ -32,18 +32,26 @@

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
{
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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 5773e76

Please sign in to comment.