php - How can I post multiple files in Laravel using Guzzle?

I am trying to upload files to an external server using JWT to authenticate. My form looks like this:
<form action="{{ route('operations.photos-for-families.upload', [$operation, $current->index]) }}" method="POST" enctype='multipart/form-data'>
<input type="file" name="photos[]" multiple="multiple">
@csrf
<input type="submit" value="submit">
</form>
My route looks like this:
Route::post('{operation}/{index}/photos-for-families/upload', function (Request $request, string $operation, int $index) {
if (!opExists($operation, $index) || !$request->has('photos') || empty($request->allFiles()))
return back()->withErrors(['Oops. Sorry, something went wrong.']);
$response = Http::attach('photos', $request->allFiles())
->post("https://cdn.mydomain.co.uk/gallery/{$operation}/{$index}/upload", [
'token' => JWT::encode([
'iss' => 'https://mydomain.co.uk',
'aud' => 'https://cdn.mydomain.co.uk',
'iat' => time(),
'exp' => time() + 30, // 30 seconds expiration in-case of intercepts or re-use
'nbf' => time() - ((60 * 60) * 24),
'data' => [
'user_id' => Auth::user()->id,
]
], 'MY_KEY')
]);
var_dump($response);
})->name('operations.photos-for-families.upload');
However, It seems I cannot send multiple photos in one single request.
InvalidArgumentException
Invalid resource type: array
I am reading the Documentation but can't find how to attach multiple photos. I know I do not verify the MimeType of the file, because its in development and this will happen once working.
Any help appreciated. The code on thecdn
server looks like this:
Route::post('/gallery/{operation}/{index}/upload', function (Request $request, string $operation, int $index) {
if(!$request->has('token'))
return redirect()->away('https://mydomain.co.uk');
try {
$access = JWT::decode($request->get('token'), 'MY_KEY', ['HS256']);
if ($access->iss !== 'https://mydomain.co.uk' ||
$access->aud !== 'https://cdn.mydomain.co.uk') {
abort(404);
}
if ($access->nbf > time())
return redirect()->away('https://mydomain.co.uk');
if ($access->exp < time())
return redirect()->away('https://mydomain.co.uk');
$files = [];
foreach ($request->allFiles() as $file):
$name = Carbon::createFromFormat('d-m-Y', Carbon::now()) . '-' . Str::random(30);
$file->storeAs("protected/{$operation}/{$index}/", "{$name}.{$file->getClientOriginalExtension()}");
tap(PhotosAccess::create([
'user_id' => $access->data->user_id,
'operation' => $operation,
'index' => $index,
'path' => "{$name}.{$file->getClientOriginalExtension()}",
]), function(PhotosAccess $photo) {
$files[] = $photo->id;
});
endforeach;
/**
* TODO: Create endpoint on mydomain as confirmation
*/
return redirect()->away('https://mydomain.co.uk/dashboard');
}
catch (Exception $e)
{
abort(404);
}
});
Answer
Solution:
You can attach multiple files by usingattach()
. Attach is a recursive function. check here
$response = Http::withToken(/*your whole JWT process*/); // will add a jwt token
foreach($request->allFiles() as $key => $files)
foreach($files as $file)
$response->attach('photos['. Str::random(20).']', file_get_contents($file->getRealPath()), Str::random(20) . '.jpg');
$response = $response->post($url);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: warning: a non-numeric value encountered in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.