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

ashashingadia's avatar

File upload using guzzle client

I am doing like following :

$request = $this->client->post($url, ['upload'=>'@'.$filepath], $options);

i want to upload file using guzzle and i am not using any form, using above post request file not getting uploaded, i just need data of uploaded file data like size, tmp_name inside $_FILES array

0 likes
15 replies
Yorki's avatar
Yorki
Best Answer
Level 11
$fileContent = File::get($filepath);
$request = $this->client->post($url, [
    'multipart' => [
        [
            'name' => 'upload',
            'contents' => $fileContent,
            'filename' => end(explode('/', $filepath)),
        ],
    ],
]);
1 like
ashashingadia's avatar

@Yorki

Thanks for your reply but I am getting Fatal error like

Class 'QuickEmailVerification\Api\File' not found

How can I solve this??

Yorki's avatar

Add use clause to your controller

use File;
1 like
ashashingadia's avatar

@Yorki, Sorry for inconvenience, but i Added that Line but now error coming like

Class 'File' not found

Yorki's avatar

Try

use Illuminate\Support\Facades\File;
Yorki's avatar

Are you even using Laravel framework to do this? If not then just use native php function

$fileContent = file_get_contents($filepath);
Yorki's avatar

Are you trying to upload file to an API, script or Laravel Controller? Is upload desired field name of uploaded file?

ashashingadia's avatar

I am trying to upload file to API, but i am not using any form, without form need to upload file.

Yorki's avatar

Is this API public? If so send me link to this endpoint documentation

ashashingadia's avatar

@Yorki If i pass path of the file then i just need data like following with file upload and i am not using any form here

 [upload] => Array
        (
            [name] => testEmail.csv
            [type] => application/octet-stream
            [tmp_name] => /tmp/php5cbtni
            [error] => 0
            [size] => 61
        )

Yorki's avatar

I don't know what are you trying to achieve exactly, can't you just use filesize() and mime_content_type()? This isn't even related to Laravel at all :)

ashashingadia's avatar

@Yorki I am not using laravel, i am using only php All i just need to upload file without from using guzzle.

Please or to participate in this conversation.