Skip to content

Commit a80cb72

Browse files
committed
When calling UploadedFile::getErrorMessage() to a file which has no error and is uploaded successfully, it should not return an error
1 parent 226bb88 commit a80cb72

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,14 @@ private static function parseFilesize(string $size): int|float
265265
/**
266266
* Returns an informative upload error message.
267267
*/
268-
public function getErrorMessage(): string
268+
public function getErrorMessage(): ?string
269269
{
270+
$errorCode = $this->error;
271+
272+
if ($errorCode === \UPLOAD_ERR_OK) {
273+
return null;
274+
}
275+
270276
static $errors = [
271277
\UPLOAD_ERR_INI_SIZE => 'The file "%s" exceeds your upload_max_filesize ini directive (limit is %d KiB).',
272278
\UPLOAD_ERR_FORM_SIZE => 'The file "%s" exceeds the upload limit defined in your form.',
@@ -277,7 +283,6 @@ public function getErrorMessage(): string
277283
\UPLOAD_ERR_EXTENSION => 'File upload was stopped by a PHP extension.',
278284
];
279285

280-
$errorCode = $this->error;
281286
$maxFilesize = \UPLOAD_ERR_INI_SIZE === $errorCode ? self::getMaxFilesize() / 1024 : 0;
282287
$message = $errors[$errorCode] ?? 'The file "%s" was not uploaded due to an unknown error.';
283288

src/Symfony/Component/HttpFoundation/Tests/File/UploadedFileTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ public function testErrorIsOkByDefault()
119119
$this->assertEquals(\UPLOAD_ERR_OK, $file->getError());
120120
}
121121

122+
public function testErrorMessageIsNullIfErrorIsOk()
123+
{
124+
$file = new UploadedFile(
125+
__DIR__.'/Fixtures/test.gif',
126+
'original.gif',
127+
'image/gif',
128+
null
129+
);
130+
131+
$this->assertEquals(null, $file->getErrorMessage());
132+
}
133+
122134
public function testGetClientOriginalName()
123135
{
124136
$file = new UploadedFile(

0 commit comments

Comments
 (0)