[Bug]: Browser plugin — file inputs named `files[]` arrive as a flat `files[]` key; multiple files overwrite each other
What Happened
In the browser plugin, a file input whose name ends in [] (<input type="file" name="files[]" multiple>, the usual way to upload several files in Laravel) never reaches the application as a file array. $request->file('files') is null; the upload is only available under the literal key files[].
LaravelHttpServer::parseMultipartBody() (v5.2.0 and current HEAD) keys files by the raw part name:
$files[$name[1]] = new UploadedFile($path, $fileName[1], $mimeType, null, true);
Ordinary fields in the same method are rebuilt through parse_str(), so items[] and a[b] nest correctly — but files skip that step. Two consequences:
files[]stays a flat key instead of['files' => [UploadedFile, ...]].- With several files under the same
files[]name, each part overwrites the previous one, so only the last file survives.
(The binary corruption of file parts through mb_ltrim() that existed in v5.1.2 is already fixed in v5.2.0 by the '8bit' encoding — thanks!)
How to Reproduce
Route:
Route::post('/upload', fn (Request $request) => response()->json([
'files' => count($request->file('files') ?? []),
'keys' => array_keys($request->allFiles()),
]));
Page with <form method="post" action="/upload" enctype="multipart/form-data"><input type="file" name="files[]" multiple><button>Send</button></form>, then in a browser test:
visit('/form')
->attach('files[]', [base_path('tests/fixtures/a.png'), base_path('tests/fixtures/b.png')])
->click('Send')
->assertSee('"files":2');
Actual: {"files":0,"keys":["files[]"]}. Expected (same as php artisan serve / a real web server): {"files":2,"keys":["files"]}.
Pest Version
pest 5.3.4, pest-plugin-browser 5.2.0 (also 5.1.2)
PHP Version
8.5
Operation System
Linux
Notes
A possible fix is to collect files into the same nested structure PHP itself builds: e.g. parse the part name with the same bracket rules as parse_str() (files[] → append to files, doc[cover] → doc['cover']), or build a placeholder query string of the file names, parse_str() it, and then replace the placeholders with the UploadedFile objects.
This looks like the case pestphp/pest-plugin-browser#286 (open) fixes. Instead of storing each file under its raw part name, it encodes every file part as <name>=<upload index> and runs the result through parse_str(), so files[] builds a list and profile[avatar] a keyed entry, the way PHP's SAPI does. A single <input type="file" name="files[]" multiple> sends one part per file with that same name, so it goes through the same path.
One gap: the PR's test uses two separate documents[] inputs, not one multiple input, so a test with your exact markup there would pin this case down.