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

Conversation

diegoscoponi
Copy link

@diegoscoponi diegoscoponi commented Nov 6, 2021

The move method always assigns a true value to the $this->hasMoved attribute regardless of whether move_uploaded_file has failed, this means that calling $file->hasMoved() always returns true.
I'm right?
Thanks!!!

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

It seems even if move_uploaded_file() only returns false, $this->hasMoved may be set to true.

https://www.php.net/manual/en/function.move-uploaded-file.php#refsect1-function.move-uploaded-file-returnvalues

Return Values ¶
Returns true on success.
If from is not a valid upload file, then no action will occur, and move_uploaded_file() will return false.
If from is a valid upload file, but cannot be moved for some reason, no action will occur, and move_uploaded_file() will return false. Additionally, a warning will be issued.

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

@diegoscoponi Thank you for PR!

We ask that contributions have code commits signed.
Could you sign?

See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/pull_request.md#signing

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

PHPUnit testing failed.

https://github.com/codeigniter4/CodeIgniter4/runs/4128729664?check_suite_focus=true

There was 1 error:

1) CodeIgniter\HTTP\Files\FileMovingTest::testMoved
TypeError: Return value of CodeIgniter\HTTP\Files\UploadedFile::hasMoved() must be of the type bool, null returned

/home/runner/work/CodeIgniter4/CodeIgniter4/system/HTTP/Files/UploadedFile.php:188
/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/HTTP/Files/FileMovingTest.php:176

Could you fix the broken test?

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Fix the failed checks in GitHub Actions, please.

If you have any questions, feel free to ask.

@kenjis kenjis changed the title Upload file move file Fix UploadedFile::move() $this->hasMoved value Nov 7, 2021
@sfadschm
Copy link
Contributor

sfadschm commented Nov 7, 2021

I might be mistaken, but if move_uploaded_file fails, the current code throws the exception so this->hasMoved willl never be set to true in that case
I am not sure if this is really a problem with the current code.

@MGatner
Copy link
Member

MGatner commented Nov 7, 2021

@sfadschm it looks like that is true some of the time, but move_uploaded_file() appears to be able to fail quietly as well. I think this PR is indeed needed as long as the issues can be addressed.

@diegoscoponi diegoscoponi requested a review from kenjis November 7, 2021 17:29
@sfadschm
Copy link
Contributor

sfadschm commented Nov 7, 2021

@sfadschm it looks like that is true some of the time, but move_uploaded_file() appears to be able to fail quietly as well. I think this PR is indeed needed as long as the issues can be addressed.

Thanks, I did not grasp that. Does 'fail silently' mean that the method simy returns false? If so, we would maybe like to throw the same exception for that case as in the catch block to have consistent output in case of failure?

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

we would maybe like to throw the same exception for that case as in the catch block to have consistent output in case of failure?

Yes.

@kenjis
Copy link
Member

kenjis commented Nov 7, 2021

@diegoscoponi
Thank you for signing.

You don't use git merge, use git rebase instead, please.
See https://github.com/codeigniter4/CodeIgniter4/blob/develop/contributing/workflow.md#pushing-your-branch

Please fix two checking failed. See
#5290 (comment)
#5290 (comment)

And see #5290 (comment)

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Please fix the above.

@kenjis kenjis added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 9, 2021
@diegoscoponi diegoscoponi requested a review from kenjis November 9, 2021 19:47
@@ -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.

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

@diegoscoponi I added a comment.

And you have a merge commit in this PR.
Please rebase your branch like the following:

$ git fetch upstream
$ git checkout UploadedFileHasMoved
$ git rebase upstream/develop
$ git push --force-with-lease origin UploadedFileHasMoved

Thank you!

@diegoscoponi diegoscoponi deleted the UploadedFileHasMoved branch November 10, 2021 01:52
@diegoscoponi diegoscoponi restored the UploadedFileHasMoved branch November 10, 2021 12:10
@diegoscoponi diegoscoponi reopened this Nov 10, 2021
@kenjis kenjis closed this in #5302 Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants