Skip to content

Commit e24e59d

Browse files
committed
Respect upload_max_filesize ini setting
1 parent 837e67d commit e24e59d

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,8 @@ $handler = function (ServerRequestInterface $request) {
757757
if ($avatar instanceof UploadedFileInterface) {
758758
if ($avatar->getError() === UPLOAD_ERR_OK) {
759759
$uploaded = $avatar->getSize() . ' bytes';
760+
} elseif ($avatar->getError() === UPLOAD_ERR_INI_SIZE) {
761+
$uploaded = 'file too large';
760762
} else {
761763
$uploaded = 'with error';
762764
}
@@ -798,6 +800,8 @@ See also [example #12](examples) for more details.
798800
> PHP's `MAX_FILE_SIZE` hidden field is respected by this middleware.
799801
800802
> This middleware respects the
803+
[`upload_max_filesize`](http://php.net/manual/en/ini.core.php#ini.upload-max-filesize)
804+
(default `2M`),
801805
[`max_input_vars`](http://php.net/manual/en/info.configuration.php#ini.max-input-vars)
802806
(default `1000`) and
803807
[`max_input_nesting_level`](http://php.net/manual/en/info.configuration.php#ini.max-input-nesting-level)

examples/12-upload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
// contents via `(string)$file->getStream()` instead.
4343
// Here, we simply use an inline image to send back to client:
4444
$avatar = '<img src="data:'. $file->getClientMediaType() . ';base64,' . base64_encode($file->getStream()) . '" /> (' . $file->getSize() . ' bytes)';
45+
} elseif ($file->getError() === UPLOAD_ERR_INI_SIZE) {
46+
$avatar = 'upload exceeds file size limit';
4547
} else {
4648
// Real applications should probably check the error number and
4749
// should print some human-friendly text

src/Io/MultipartParser.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)