Skip to content

Commit 900778b

Browse files
committed
Fixed the regex
1 parent 4fb60b4 commit 900778b

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/Http/Request.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -282,21 +282,23 @@ private function addFile(array &$files, string $fieldName, string $filename, str
282282
'size' => filesize($tmpPath),
283283
];
284284

285-
if (!preg_match('|([^\[]+)(?:\[([^\]]+)\])*(\[\])?|', $fieldName, $matches)) {
285+
$matches = preg_split('|(\[[^\]]*\])|', $fieldName, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
286+
$count = count($matches);
287+
if (1 === $count) {
286288
$files[$fieldName] = $data;
287289
} else {
288290
$current = &$files;
289-
$count = count($matches);
290-
for ($i = 1; $i < $count; $i++) {
291-
if (empty($matches[$i])) {
291+
foreach ($matches as $i => $match) {
292+
if ($match === '[]') {
293+
$current[] = $data;
292294
continue;
293295
}
294-
if ($matches[$i] === '[]') {
295-
$current[] = $data;
296-
} elseif ($i === $count -1) {
297-
$current[$matches[$i]] = $data;
296+
297+
$trimmedMatch = trim($match, '[]');
298+
if ($i === $count -1) {
299+
$current[$trimmedMatch] = $data;
298300
} else {
299-
$current = &$current[$matches[$i]];
301+
$current = &$current[$trimmedMatch];
300302
}
301303
}
302304
}

test/Http/RequestTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,16 @@ public function testMultipartContentWithMultipleFiles()
192192
???
193193
IHDR??? ??? ?????? ???? IDATxc???51?)?:??????IEND?B`?
194194
--578de3b0e3c46.2334ba3
195-
Content-Disposition: form-data; name="four[item_a][item_b[]"; filename="foo.png"
195+
Content-Disposition: form-data; name="four[item_a][item_b][]"; filename="foo.png"
196196
Content-Length: 71
197197
Content-Type: image/png
198198
199199
?PNG
200200

201201
???
202202
IHDR??? ??? ?????? ???? IDATxc???51?)?:??????IEND?B`?
203-
Content-Disposition: form-data; name="four[item_a][item_b[]"; filename="bar.png"
203+
--578de3b0e3c46.2334ba3
204+
Content-Disposition: form-data; name="four[item_a][item_b][]"; filename="bar.png"
204205
Content-Length: 71
205206
Content-Type: image/png
206207
@@ -246,13 +247,13 @@ public function testMultipartContentWithMultipleFiles()
246247
$this->assertNotEmpty($files['four']);
247248
$this->assertNotEmpty($files['four']['item_a']);
248249
$this->assertNotEmpty($files['four']['item_a']['item_b']);
249-
$this->assertCount(2, $files['three']['item_a']['item_b']);
250+
$this->assertCount(2, $files['four']['item_a']['item_b']);
250251

251252
// Check the HttpFoundation request
252253
rewind($stream);
253254
$httpFoundationRequest = $request->getHttpFoundationRequest();
254255
$this->assertEquals($expectedPost, $httpFoundationRequest->request->all());
255-
$this->assertCount(7, $httpFoundationRequest->files->all());
256+
$this->assertCount(4, $httpFoundationRequest->files->all());
256257

257258
}
258259
}

0 commit comments

Comments
 (0)