Skip to content

[HttpFoundation] When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error #54304

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
30 changes: 21 additions & 9 deletions src/Symfony/Component/HttpFoundation/File/UploadedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,22 @@ public function move(string $directory, ?string $name = null): File

switch ($this->error) {
case \UPLOAD_ERR_INI_SIZE:
throw new IniSizeFileException($this->getErrorMessage());
throw new IniSizeFileException($this->getExceptionMessage());
case \UPLOAD_ERR_FORM_SIZE:
throw new FormSizeFileException($this->getErrorMessage());
throw new FormSizeFileException($this->getExceptionMessage());
case \UPLOAD_ERR_PARTIAL:
throw new PartialFileException($this->getErrorMessage());
throw new PartialFileException($this->getExceptionMessage());
case \UPLOAD_ERR_NO_FILE:
throw new NoFileException($this->getErrorMessage());
throw new NoFileException($this->getExceptionMessage());
case \UPLOAD_ERR_CANT_WRITE:
throw new CannotWriteFileException($this->getErrorMessage());
throw new CannotWriteFileException($this->getExceptionMessage());
case \UPLOAD_ERR_NO_TMP_DIR:
throw new NoTmpDirFileException($this->getErrorMessage());
throw new NoTmpDirFileException($this->getExceptionMessage());
case \UPLOAD_ERR_EXTENSION:
throw new ExtensionFileException($this->getErrorMessage());
throw new ExtensionFileException($this->getExceptionMessage());
}

throw new FileException($this->getErrorMessage());
throw new FileException($this->getExceptionMessage());
}

/**
Expand Down Expand Up @@ -265,7 +265,7 @@ private static function parseFilesize(string $size): int|float
/**
* Returns an informative upload error message.
*/
public function getErrorMessage(): string
private function getExceptionMessage(): string
{
static $errors = [
\UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
Expand All @@ -283,4 +283,16 @@ public function getErrorMessage(): string

return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
}

/**
* Retrieves a user-friendly error message for file upload issues, if any.
*/
public function getErrorMessage(): string
{
if (\UPLOAD_ERR_OK === $this->error) {
return '';
}

return $this->getExceptionMessage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ public function testErrorIsOkByDefault()
$this->assertEquals(\UPLOAD_ERR_OK, $file->getError());
}

public function testInvalidFile()
{
$this->expectException(FileException::class);
$this->expectExceptionMessage('The file "original.gif" was not uploaded due to an unknown error.');

$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
);

$file->move(__DIR__.'/Fixtures/directory');
}

public function testNoErrorMessageIfErrorIsUploadErrOk()
{
$file = new UploadedFile(
__DIR__.'/Fixtures/test.gif',
'original.gif',
'image/gif',
null
);

$this->assertEquals('', $file->getErrorMessage());
}

public function testGetClientOriginalName()
{
$file = new UploadedFile(
Expand Down