1

Uploading multiple images in larvel rest api from postman/curl are not able work. It only insert one image.

Using larvel upload form in view has no issue at all, it work fine.

<input type="file" name="image[]" multiple />

WHat i am trying is to upload multiple images with Postman and curl in Laravel rest api.

THis is controller file

public function uploadimages(Request $request){
            $files = $request->allFiles('image');       
            $count = 0;       
            foreach ($files as $file) {
                $file->store('public/uploads');
                $count++;
                //this technic also not work
                /* $name= $file->getClientOriginalName();
                   $file->move('public/uploads', $name);
                   $images[]=$name;*/
           }
           //$count return only 1(it only upload one file)
           return response()->json($count, 201);
    }

This is my curl command

curl -X POST  http://localhost:8000/api/uploadimages -H "Content-Type: multipart/form-data" -F "image=@/C:\xampp\htdocs\2.jpg" -F "image=@/C:\xampp\htdocs\1.jpg"

this command insert only one file.

i also put this and tried "image[]=@/C:\xampp\htdocs\1.jpg" and get the error.

 curl -X POST  http://localhost:8000/api/uploadimages -H "Content-Type: multipart/form-data" -F "image[]=@/C:\xampp\htdocs\2.jpg" -F "image[]=@/C:\xampp\htdocs\1.jpg"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (26) couldn't open file "/C:\xampp\htdocs\2.jpg"

In postman application also only upload one file. i double check about postman setting for uploading file from other question. i think setting is ok.

i cant find uploading multiple images in Larvel Rest api.

What is the issue there, please somebody suggest me and explain me about the issues.

1 Answer 1

2

I think your problem is that you're trying to send 2 files with the same key. image in this case. Try changing second file key to something else like image2 so it would be something like

curl -X POST  http://localhost:8000/api/uploadimages -H "Content-Type: multipart/form-data" -F "image=@/C:\xampp\htdocs\2.jpg" -F "image2=@/C:\xampp\htdocs\1.jpg"
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @avpav, it worked. i hav been stuck for many day. You help me. Thanks a lot.
one thing, do you have any idea suggestion why is it not working in postman application?
I don't know. Can you provide a screenshot?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.