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

shing_shing's avatar

Uploading multiple files to another api link using Laravel 6 and GuzzleHttp

Good day! I've been searching for answers that could help me with my assignment. But couldn't find any.

Framework: Laravel 6 Domain sample api: https://sample.net/api/uploadtocloud (this is just an example, this doesn't exist)

We need to store the files uploaded from our system to another hosting (with the sample receiving link), because we believe our system storage couldn't handle the number of the files to be uploaded later.

I've tried this one:

$client = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
$response = $client->request('POST', $sample_api [
                    'headers' => [
                        'Accept' => 'application/json',
                        'Content-Type'          => 'multipart/form-data',
                        // 'Accept-Encoding' => 'gzip, deflate, br',
                        // 'Content-Type' => 'application/x-www-form-urlencoded',
                        // 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
                        'X-CSRF-TOKEN' => $request->get('_token')
                    ],
                    'multipart' => [
                        [
                             'name'     => 'file_name',
                             'contents' => fopen('/path/to/file', 'r')
                        ]
                    ],
                    'debug' => true
]);

but it returns a permission issue

I tried to store it in our system and get the new file location to avoid the permission issue:

                $client = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
                $response = $client->request('POST', $sample_api [
                    'headers' => [
                        'Accept' => 'application/json',
                        'Content-Type'          => 'multipart/form-data',
                        // 'Accept-Encoding' => 'gzip, deflate, br',
                        // 'Content-Type' => 'application/x-www-form-urlencoded',
                        // 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0',
                        'X-CSRF-TOKEN' => $request->get('_token')
                    ],
                    'multipart' => [
                        [
                            'name' => $filename,
                            'extension' => $extension,
                            'file' => $file,
                            'contents' => file_get_contents($newpathoftheuploadedfile, 'r'),
                            'filesize' => $filesize
                        ]
                    ],
                    'debug' => true
                ]);

but it returns and error exception of "curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE* "

What seems to be the problem?

0 likes
3 replies
tisuchi's avatar

@shing_shing It looks like the problem is with the way you are trying to open the file. In the first example, you are trying to open the file using the path '/path/to/file' which may not have the correct permissions for the script to read. In the second example, you are trying to use the file_get_contents() function to read the contents of the file, but you are passing it a path to the file instead of a valid file resource.

You can try to use Laravel's Storage facade to store the files and get the correct file path to use in the Guzzle request. For example:

use Illuminate\Support\Facades\Storage;

$path = $request->file('file_name')->store('uploaded_files');
$file = Storage::get($path);

$response = $client->request('POST', $sample_api, [
    'headers' => [
        'Content-Type' => 'multipart/form-data',
    ],
    'multipart' => [
        [
            'name' => 'file_name',
            'contents' => $file
        ]
    ],
]);

It is also important to make sure that your storage directory has the correct permissions for writing. You can check the documentation of Laravel's Storage facade for more information.

shing_shing's avatar

@tisuchi good day, Sir! thank you for the response and for explaining it to me. I will try it now. Thank you so much.

1 like
shing_shing's avatar

Good day!

Hello, Sir @tisuchi. Once again, thank you for the advise, I tried the code and the proper way to get the file location. This is what I did:


                $filename = $file->getClientOriginalName();                
                $extension = $file->getClientOriginalExtension();
                $path = $file->storeAs('uploaded_files',$filename);
                $file = Storage::get($path);
                
                $client = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
                $response = $client->request('POST', $sampleapi, [
                    'headers' => [
                        'Content-Type' => 'multipart/form-data',
                    ],
                    'multipart' => [
                        [
                            'name' => $filename,
                            'contents' => $file
                        ]
                    ]
                ]);

it returns this: GuzzleHttp\Exception\ClientException Client error: POST (the sample api) resulted in a 419 unknown status response: $(docume (truncated...)

I also added the "debug => true"

                $response = $client->request('POST', $sampleapi, [
                    'headers' => [
                        'Content-Type' => 'multipart/form-data',
                    ],
                    'multipart' => [
                        [
                            'name' => $filename,
                            'contents' => $file
                        ]
                    ]
                    'debug' => true
                ]);

It returns this one: curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*

What am I doing wrong?

Tried this one:


                    'multipart' => [
                        [
                            'name' => $filename,
                            'contents' => fopen($file,'r')
                        ]
                        ],

and this one , from this reference link: Reference link: https://stackoverflow.com/questions/16597392/how-do-you-get-the-path-to-the-laravel-storage-folder


$file_path =Storage::get( storage_path('app/'.$file));

                    'multipart' => [
                        [
                            'name' => $filename,
                            'contents' => fopen($file_path ,'r')
                        ]
                        ],

still curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*

Please or to participate in this conversation.