Skip to content

Commit c680d19

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 a8c43b6 commit c680d19

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -201,22 +201,22 @@ public function move(string $directory, ?string $name = null): File
201201

202202
switch ($this->error) {
203203
case \UPLOAD_ERR_INI_SIZE:
204-
throw new IniSizeFileException($this->getErrorMessage());
204+
throw new IniSizeFileException($this->getExceptionMessage());
205205
case \UPLOAD_ERR_FORM_SIZE:
206-
throw new FormSizeFileException($this->getErrorMessage());
206+
throw new FormSizeFileException($this->getExceptionMessage());
207207
case \UPLOAD_ERR_PARTIAL:
208-
throw new PartialFileException($this->getErrorMessage());
208+
throw new PartialFileException($this->getExceptionMessage());
209209
case \UPLOAD_ERR_NO_FILE:
210-
throw new NoFileException($this->getErrorMessage());
210+
throw new NoFileException($this->getExceptionMessage());
211211
case \UPLOAD_ERR_CANT_WRITE:
212-
throw new CannotWriteFileException($this->getErrorMessage());
212+
throw new CannotWriteFileException($this->getExceptionMessage());
213213
case \UPLOAD_ERR_NO_TMP_DIR:
214-
throw new NoTmpDirFileException($this->getErrorMessage());
214+
throw new NoTmpDirFileException($this->getExceptionMessage());
215215
case \UPLOAD_ERR_EXTENSION:
216-
throw new ExtensionFileException($this->getErrorMessage());
216+
throw new ExtensionFileException($this->getExceptionMessage());
217217
}
218218

219-
throw new FileException($this->getErrorMessage());
219+
throw new FileException($this->getExceptionMessage());
220220
}
221221

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

284284
return sprintf($message, $this->getClientOriginalName(), $maxFilesize);
285285
}
286+
287+
/**
288+
* Retrieves a user-friendly error message for file upload issues, if any.
289+
*/
290+
public function getErrorMessage(): string
291+
{
292+
if (\UPLOAD_ERR_OK === $this->error) {
293+
return '';
294+
}
295+
296+
return $this->getExceptionMessage();
297+
}
286298
}

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

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

122+
public function testInvalidFile()
123+
{
124+
$this->expectException(FileException::class);
125+
$this->expectExceptionMessage('The file "original.gif" was not uploaded due to an unknown error.');
126+
127+
$file = new UploadedFile(
128+
__DIR__.'/Fixtures/test.gif',
129+
'original.gif',
130+
'image/gif',
131+
);
132+
133+
$file->move(__DIR__.'/Fixtures/directory');
134+
}
135+
136+
public function testNoErrorMessageIfErrorIsUploadErrOk()
137+
{
138+
$file = new UploadedFile(
139+
__DIR__.'/Fixtures/test.gif',
140+
'original.gif',
141+
'image/gif',
142+
null
143+
);
144+
145+
$this->assertEquals('', $file->getErrorMessage());
146+
}
147+
122148
public function testGetClientOriginalName()
123149
{
124150
$file = new UploadedFile(

0 commit comments

Comments
 (0)