@@ -47,6 +47,13 @@ final class MultipartParser
4747 */
4848 private $ maxInputNestingLevel = 64 ;
4949
50+ /**
51+ * ini setting "upload_max_filesize"
52+ *
53+ * @var int
54+ */
55+ private $ uploadMaxFilesize ;
56+
5057 private $ postCount = 0 ;
5158
5259 public function __construct ()
@@ -59,6 +66,8 @@ public function __construct()
5966 if ($ var !== false ) {
6067 $ this ->maxInputNestingLevel = (int )$ var ;
6168 }
69+
70+ $ this ->uploadMaxFilesize = $ this ->iniUploadMaxFilesize ();
6271 }
6372
6473 public function parse (ServerRequestInterface $ request )
@@ -157,6 +166,17 @@ private function parseUploadedFile($filename, $contentType, $contents)
157166 );
158167 }
159168
169+ // file exceeds "upload_max_filesize" ini setting
170+ if ($ size > $ this ->uploadMaxFilesize ) {
171+ return new UploadedFile (
172+ Psr7 \stream_for ('' ),
173+ $ size ,
174+ UPLOAD_ERR_INI_SIZE ,
175+ $ filename ,
176+ $ contentType
177+ );
178+ }
179+
160180 // file exceeds MAX_FILE_SIZE value
161181 if ($ this ->maxFileSize !== null && $ size > $ this ->maxFileSize ) {
162182 return new UploadedFile (
@@ -269,4 +289,28 @@ private function extractPost($postFields, $key, $value)
269289
270290 return $ postFields ;
271291 }
292+
293+ /**
294+ * Gets upload_max_filesize from PHP's configuration expressed in bytes
295+ *
296+ * @return int
297+ * @link http://php.net/manual/en/ini.core.php#ini.upload-max-filesize
298+ * @codeCoverageIgnore
299+ */
300+ private function iniUploadMaxFilesize ()
301+ {
302+ $ size = ini_get ('upload_max_filesize ' );
303+ $ suffix = strtoupper (substr ($ size , -1 ));
304+ if ($ suffix === 'K ' ) {
305+ return substr ($ size , 0 , -1 ) * 1024 ;
306+ }
307+ if ($ suffix === 'M ' ) {
308+ return substr ($ size , 0 , -1 ) * 1024 * 1024 ;
309+ }
310+ if ($ suffix === 'G ' ) {
311+ return substr ($ size , 0 , -1 ) * 1024 * 1024 * 1024 ;
312+ }
313+
314+ return $ size ;
315+ }
272316}
0 commit comments