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

afoysal's avatar

Upload file using GuzzleHttp

I am trying to Upload file using GuzzleHttp. My Code is like below

        $image_path = $request->file('company_logo')->getPathname();
        $image_mime = $request->file('company_logo')->getmimeType();
        $image_org  = $request->file('company_logo')->getClientOriginalName();

        $new_client = new \GuzzleHttp\Client();

        $response = $new_client->post( 'https://sometext/profile-update', [
            'multipart' => [
                [
                    'name'     => 'company_logo',
                    'filename' => $image_org,
                    'Mime-Type'=> $image_mime,
                    'contents' => fopen( $image_path, 'r' ),
                ],
                'headers' => [
                    'Accept' => 'application/json',
                    'Authorization' => 'Bearer ' . Session::get('SesTok'),
                ], 
                [
                    "first_name" => $request->first_name,
                    "last_name" => $request->last_name,
                    'email' => $request->profile_email, 
                ],  
            ],
        ]);   

But I am getting A 'contents' key is required error .

error

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

The value of multipart is an array of associative arrays, each containing the following key value pairs:

  • name: (string, required) the form field name
  • contents: (StreamInterface/resource/string, required) The data to use in the form element.
  • headers: (array) Optional associative array of custom headers to use with the form element.
  • filename: (string) Optional string to send as the filename in the part.

So it should be something more like the following:

$response = $new_client->post( 'https://sometext/profile-update', [
    'headers' => [
        'Accept' => 'application/json',
        'Authorization' => 'Bearer ' . Session::get('SesTok'),
    ],
    'multipart' => [
        [
            'name'     => 'company_logo',
            'filename' => $image_org,
            'contents' => fopen( $image_path, 'r' ),
        ],
        [
            'name' => "first_name",
			'contents' =>  => $request->first_name,
        ],
        [
            'name' => "last_name",
			'contents' =>  => $request->last_name,
        ],
        [
            'name' => "email",
			'contents' =>  => $request->profile_email,
        ],
    ]
]);
1 like

Please or to participate in this conversation.