Skip to content

Commit c1e9c32

Browse files
committed
Upload fields left blank on submission do not count towards max uploads
1 parent 85fd8f9 commit c1e9c32

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,8 @@ By default, this middleware respects the
800800
[`max_file_uploads`](http://php.net/manual/en/ini.core.php#ini.max-file-uploads)
801801
(default `20`) ini setting.
802802
If you upload more files in a single request, additional files will be ignored
803-
and the `getUploadedFiles()` method returns a truncated array.
803+
and the `getUploadedFiles()` method returns a truncated array.
804+
Note that upload fields left blank on submission do not count towards this limit.
804805
You can control the maximum number of file uploads per request by explicitly
805806
passing the second parameter to the constructor like this:
806807

src/Io/MultipartParser.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,6 @@ private function parseFile($name, $filename, $contentType, $contents)
172172

173173
private function parseUploadedFile($filename, $contentType, $contents)
174174
{
175-
// ignore excessive number of file uploads
176-
if (++$this->filesCount > $this->maxFileUploads) {
177-
return;
178-
}
179-
180175
$size = strlen($contents);
181176

182177
// no file selected (zero size and empty filename)
@@ -190,6 +185,11 @@ private function parseUploadedFile($filename, $contentType, $contents)
190185
);
191186
}
192187

188+
// ignore excessive number of file uploads
189+
if (++$this->filesCount > $this->maxFileUploads) {
190+
return;
191+
}
192+
193193
// file exceeds "upload_max_filesize" ini setting
194194
if ($size > $this->uploadMaxFilesize) {
195195
return new UploadedFile(

tests/Io/MultipartParserTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,54 @@ public function testUploadTooManyFilesReturnsTruncatedList()
790790
$this->assertSame('hello', (string)$file->getStream());
791791
}
792792

793+
public function testUploadTooManyFilesIgnoresEmptyFilesAndIncludesThemDespiteTruncatedList()
794+
{
795+
$boundary = "---------------------------12758086162038677464950549563";
796+
797+
$data = "--$boundary\r\n";
798+
$data .= "Content-Disposition: form-data; name=\"first\"; filename=\"first\"\r\n";
799+
$data .= "Content-type: text/plain\r\n";
800+
$data .= "\r\n";
801+
$data .= "hello\r\n";
802+
$data .= "--$boundary\r\n";
803+
$data .= "Content-Disposition: form-data; name=\"empty\"; filename=\"\"\r\n";
804+
$data .= "Content-type: text/plain\r\n";
805+
$data .= "\r\n";
806+
$data .= "\r\n";
807+
$data .= "--$boundary\r\n";
808+
$data .= "Content-Disposition: form-data; name=\"second\"; filename=\"second\"\r\n";
809+
$data .= "Content-type: text/plain\r\n";
810+
$data .= "\r\n";
811+
$data .= "world\r\n";
812+
$data .= "--$boundary--\r\n";
813+
814+
$request = new ServerRequest('POST', 'http://example.com/', array(
815+
'Content-Type' => 'multipart/form-data; boundary=' . $boundary,
816+
), $data, 1.1);
817+
818+
$parser = new MultipartParser(100, 1);
819+
$parsedRequest = $parser->parse($request);
820+
821+
$files = $parsedRequest->getUploadedFiles();
822+
823+
$this->assertCount(2, $files);
824+
$this->assertTrue(isset($files['first']));
825+
$this->assertTrue(isset($files['empty']));
826+
827+
$file = $files['first'];
828+
$this->assertSame('first', $file->getClientFilename());
829+
$this->assertSame('text/plain', $file->getClientMediaType());
830+
$this->assertSame(5, $file->getSize());
831+
$this->assertSame(UPLOAD_ERR_OK, $file->getError());
832+
$this->assertSame('hello', (string)$file->getStream());
833+
834+
$file = $files['empty'];
835+
$this->assertSame('', $file->getClientFilename());
836+
$this->assertSame('text/plain', $file->getClientMediaType());
837+
$this->assertSame(0, $file->getSize());
838+
$this->assertSame(UPLOAD_ERR_NO_FILE, $file->getError());
839+
}
840+
793841
public function testPostMaxFileSize()
794842
{
795843
$boundary = "---------------------------12758086162038677464950549563";

0 commit comments

Comments
 (0)