Skip to content

Commit ff34e93

Browse files
author
Matthias Lill
committed
Error handling using validation function. Returns error message on failure.
1 parent 3f8ada1 commit ff34e93

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Model/Behavior/BlobFileBehavior.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public function isUploadedFileImage($model,$params) {
6969

7070
$val = array_shift($params);
7171

72+
if ( ! empty($val['error'])) {
73+
return $this->errorCodeToMessage($val['error']);
74+
}
75+
7276
if ( ! empty($val['name']) and empty($val['tmp_name'])) {
7377
return false;
7478
}
@@ -83,4 +87,36 @@ public function isUploadedFileImage($model,$params) {
8387
return $fileHandler->isImage();
8488
}
8589

90+
protected function errorCodeToMessage($code)
91+
{
92+
switch ($code) {
93+
case UPLOAD_ERR_INI_SIZE:
94+
$message = "The uploaded file exceeds the maximum allowed upload size of ".ini_get('upload_max_filesize').'.'; // upload_max_filesize directive in php.ini
95+
break;
96+
case UPLOAD_ERR_FORM_SIZE:
97+
$message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
98+
break;
99+
case UPLOAD_ERR_PARTIAL:
100+
$message = "The uploaded file was only partially uploaded";
101+
break;
102+
case UPLOAD_ERR_NO_FILE:
103+
$message = "No file was uploaded";
104+
break;
105+
case UPLOAD_ERR_NO_TMP_DIR:
106+
$message = "Missing a temporary folder";
107+
break;
108+
case UPLOAD_ERR_CANT_WRITE:
109+
$message = "Failed to write file to disk";
110+
break;
111+
case UPLOAD_ERR_EXTENSION:
112+
$message = "File upload stopped by extension";
113+
break;
114+
115+
default:
116+
$message = "Unknown upload error";
117+
break;
118+
}
119+
return $message;
120+
}
121+
86122
}

0 commit comments

Comments
 (0)