From 3221ab076962eaaa1d4286ce751f8ff8ea934e95 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 10 Dec 2018 11:49:36 +0300 Subject: [PATCH] Fix #15683: Fix file as array uploading in MultipartFormDataParser (#16950) --- framework/CHANGELOG.md | 1 + framework/web/MultipartFormDataParser.php | 3 ++- .../web/MultipartFormDataParserTest.php | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index b078c878a2..776693585a 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.16 under development ------------------------ +- Bug #15683: Fixed file as array uploading in MultipartFormDataParser (Groonya) - Bug #16822: Create config dir recursively in message/config (Groonya) - Bug #16580: Delete unused php message files in MessageController if `$removeUnused` option is on (Groonya) - Bug #16022: Fix UniqueValidator for PostgreSQL. Checks the uniqueness of keys in `jsonb` field (lav45) diff --git a/framework/web/MultipartFormDataParser.php b/framework/web/MultipartFormDataParser.php index 1dc6b314cd..c9b7bd039a 100644 --- a/framework/web/MultipartFormDataParser.php +++ b/framework/web/MultipartFormDataParser.php @@ -320,7 +320,8 @@ class MultipartFormDataParser extends BaseObject implements RequestParserInterfa $namePart = trim($namePart, ']'); if ($namePart === '') { $current[] = []; - $lastKey = array_pop(array_keys($current)); + $keys = array_keys($current); + $lastKey = array_pop($keys); $current = &$current[$lastKey]; } else { if (!isset($current[$namePart])) { diff --git a/tests/framework/web/MultipartFormDataParserTest.php b/tests/framework/web/MultipartFormDataParserTest.php index cf3888fa11..3ab5482542 100644 --- a/tests/framework/web/MultipartFormDataParserTest.php +++ b/tests/framework/web/MultipartFormDataParserTest.php @@ -129,6 +129,23 @@ class MultipartFormDataParserTest extends TestCase $this->assertEquals(UPLOAD_ERR_INI_SIZE, $_FILES['thirdFile']['error']); } + public function testUploadFileAsArray(){ + $parser = new MultipartFormDataParser(); + + $boundary = '---------------------------22472926011618'; + $contentType = 'multipart/form-data; boundary=' . $boundary; + $rawBody = "--{$boundary}\nContent-Disposition: form-data; name=\"someFile[]\"; filename=\"some-file.txt\"\nContent-Type: text/plain\r\n\r\nsome file content"; + $rawBody .= "--{$boundary}--"; + + $parser->parse($rawBody, $contentType); + + $this->assertNotEmpty($_FILES['someFile']); + $this->assertEquals(UPLOAD_ERR_OK, $_FILES['someFile']['error'][0]); + $this->assertEquals('some-file.txt', $_FILES['someFile']['name'][0]); + $this->assertEquals('text/plain', $_FILES['someFile']['type'][0]); + $this->assertStringEqualsFile($_FILES['someFile']['tmp_name'][0], 'some file content'); + } + /** * @depends testNotEmptyPost * @depends testNotEmptyFiles