Skip to content

Commit a2474ad

Browse files
[2.7] Improve uploaded file error message (#621)
* improve error message * php7 compatibility * formatting * formatting * Update UploadedFile.php * Made things much more efficient --------- Co-authored-by: Graham Campbell <GrahamCampbell@users.noreply.github.com>
1 parent ca06f23 commit a2474ad

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/UploadedFile.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111

1212
class UploadedFile implements UploadedFileInterface
1313
{
14-
private const ERRORS = [
15-
UPLOAD_ERR_OK,
16-
UPLOAD_ERR_INI_SIZE,
17-
UPLOAD_ERR_FORM_SIZE,
18-
UPLOAD_ERR_PARTIAL,
19-
UPLOAD_ERR_NO_FILE,
20-
UPLOAD_ERR_NO_TMP_DIR,
21-
UPLOAD_ERR_CANT_WRITE,
22-
UPLOAD_ERR_EXTENSION,
14+
private const ERROR_MAP = [
15+
UPLOAD_ERR_OK => 'UPLOAD_ERR_OK',
16+
UPLOAD_ERR_INI_SIZE => 'UPLOAD_ERR_INI_SIZE',
17+
UPLOAD_ERR_FORM_SIZE => 'UPLOAD_ERR_FORM_SIZE',
18+
UPLOAD_ERR_PARTIAL => 'UPLOAD_ERR_PARTIAL',
19+
UPLOAD_ERR_NO_FILE => 'UPLOAD_ERR_NO_FILE',
20+
UPLOAD_ERR_NO_TMP_DIR => 'UPLOAD_ERR_NO_TMP_DIR',
21+
UPLOAD_ERR_CANT_WRITE => 'UPLOAD_ERR_CANT_WRITE',
22+
UPLOAD_ERR_EXTENSION => 'UPLOAD_ERR_EXTENSION',
2323
];
2424

2525
/**
@@ -104,7 +104,7 @@ private function setStreamOrFile($streamOrFile): void
104104
*/
105105
private function setError(int $error): void
106106
{
107-
if (false === in_array($error, UploadedFile::ERRORS, true)) {
107+
if (!isset(UploadedFile::ERROR_MAP[$error])) {
108108
throw new InvalidArgumentException(
109109
'Invalid error status for UploadedFile'
110110
);
@@ -137,7 +137,7 @@ public function isMoved(): bool
137137
private function validateActive(): void
138138
{
139139
if (false === $this->isOk()) {
140-
throw new RuntimeException('Cannot retrieve stream due to upload error');
140+
throw new RuntimeException(\sprintf('Cannot retrieve stream due to upload error (%s)', self::ERROR_MAP[$this->error]));
141141
}
142142

143143
if ($this->isMoved()) {

0 commit comments

Comments
 (0)