Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GSTAR's avatar
Level 1

Laravel HTTP Client - upload a file into $_FILES array

I am using the HTTP Client in Laravel 8. I am making a Curl request to an endpoint (in another framework) which expects a file from the $_FILES array, i.e:

$file = $_FILES['csv_file'];

Here is my Laravel code:

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

public function index(Request $request)
{
    $url = 'my-url';
    $post_data = $request->post();

    $contents = fopen($request->file('csv_file'), 'r');

    $response = Http::attach('csv_file', $contents)
        ->asForm()
        ->post($url, $post_data);

    return $response->body();
}

However after performing the Curl request, in my other application $_FILES is empty. I can see that $_POST is populated with POST data but it does not appear to be populating the file in the $_FILES array.

I have checked the value of $contents and it does contain a stream resource.

Anyone know how to do this?

0 likes
1 reply
tykus's avatar
tykus
Best Answer
Level 104

Don't use asForm(); it is replacing the body format in the Http Request which was previously set as multipart by attach():

$response = Http::attach('csv_file', $contents->post($url, $post_data);

Please or to participate in this conversation.