@@ -85,19 +85,40 @@ protected function validateValue($file)
8585 switch ($ file ->error ) {
8686 case UPLOAD_ERR_OK :
8787 if ($ this ->maxSize !== null && $ file ->size > $ this ->maxSize ) {
88- return [$ this ->tooBig , ['file ' => $ file ->name , 'limit ' => $ this ->getSizeLimit ()]];
88+ return [
89+ $ this ->tooBig ,
90+ [
91+ 'file ' => $ file ->name ,
92+ 'limit ' => $ this ->getSizeLimit (),
93+ 'formattedLimit ' => Yii::$ app ->formatter ->asShortSize ($ this ->getSizeLimit ()),
94+ ]
95+ ];
8996 } elseif ($ this ->minSize !== null && $ file ->size < $ this ->minSize ) {
90- return [$ this ->tooSmall , ['file ' => $ file ->name , 'limit ' => $ this ->minSize ]];
97+ return [
98+ $ this ->tooSmall ,
99+ [
100+ 'file ' => $ value ->name ,
101+ 'limit ' => $ this ->minSize ,
102+ 'formattedLimit ' => Yii::$ app ->formatter ->asShortSize ($ this ->minSize ),
103+ ],
104+ ];
91105 } elseif (!empty ($ this ->extensions ) && !$ this ->validateExtension ($ file )) {
92106 return [$ this ->wrongExtension , ['file ' => $ file ->name , 'extensions ' => implode (', ' , $ this ->extensions )]];
93- } elseif (!empty ($ this ->mimeTypes ) && !in_array ( $ file -> type , $ this ->mimeTypes , false )) {
107+ } elseif (!empty ($ this ->mimeTypes ) && !$ this ->validateMimeType ( $ file )) {
94108 return [$ this ->wrongMimeType , ['file ' => $ file ->name , 'mimeTypes ' => implode (', ' , $ this ->mimeTypes )]];
95109 }
96-
110+
97111 return null ;
98112 case UPLOAD_ERR_INI_SIZE :
99113 case UPLOAD_ERR_FORM_SIZE :
100- return [$ this ->tooBig , ['file ' => $ file ->name , 'limit ' => $ this ->getSizeLimit ()]];
114+ return [
115+ $ this ->tooBig ,
116+ [
117+ 'file ' => $ file ->name ,
118+ 'limit ' => $ this ->getSizeLimit (),
119+ 'formattedLimit ' => Yii::$ app ->formatter ->asShortSize ($ this ->getSizeLimit ()),
120+ ]
121+ ];
101122 case UPLOAD_ERR_PARTIAL :
102123 Yii::warning ('File was only partially uploaded: ' . $ file ->name , __METHOD__ );
103124 break ;
@@ -136,4 +157,22 @@ protected function validateExtension($file)
136157 }
137158 return true ;
138159 }
160+
161+ private function buildMimeTypeRegexp ($ mask )
162+ {
163+ return '/^ ' . str_replace ('\* ' , '.* ' , preg_quote ($ mask , '/ ' )) . '$/ ' ;
164+ }
165+
166+ protected function validateMimeType ($ file )
167+ {
168+ foreach ($ this ->mimeTypes as $ mimeType ) {
169+ if ($ mimeType === $ file ->type ) {
170+ return true ;
171+ }
172+ if (strpos ($ mimeType , '* ' ) !== false && preg_match ($ this ->buildMimeTypeRegexp ($ mimeType ), $ file ->type )) {
173+ return true ;
174+ }
175+ }
176+ return false ;
177+ }
139178}
0 commit comments