Skip to content

Fix UploadedFile::move() $this->hasMoved value #5290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions system/HTTP/Files/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public function move(string $targetPath, ?string $name = null, bool $overwrite =
$destination = $overwrite ? $targetPath . $name : $this->getDestination($targetPath . $name);

try {
move_uploaded_file($this->path, $destination);
$this->hasMoved = move_uploaded_file($this->path, $destination);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When move_uploaded_file() returns false, the current code returns false after all.
But we need to throw HTTPException::forMoveFailed() as the same as move_uploaded_file() raise warning
to have consistent output in case of failure.

Please change the code like that.

} catch (Exception $e) {
$error = error_get_last();
$message = isset($error['message']) ? strip_tags($error['message']) : '';
Expand All @@ -152,11 +152,10 @@ public function move(string $targetPath, ?string $name = null, bool $overwrite =
@chmod($targetPath, 0777 & ~umask());

// Success, so store our new information
$this->path = $targetPath;
$this->name = basename($destination);
$this->hasMoved = true;
$this->path = $targetPath;
$this->name = basename($destination);

return true;
return $this->hasMoved;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/system/HTTP/Files/FileMovingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function testMoved()
$this->assertInstanceOf(UploadedFile::class, $file);
$this->assertFalse($file->hasMoved());
$file->move($destination, $file->getName(), false);
$this->assertTrue($file->hasMoved());
$this->assertIsBool($file->hasMoved());
}

public function testStore()
Expand Down Expand Up @@ -315,6 +315,8 @@ function move_uploaded_file($filename, $destination)
{
copy($filename, $destination);
unlink($filename);

return mt_rand(0, 1);
}

function rrmdir($src)
Expand Down