$request->file() doesn't return an array of files

Ivan Hervé posted 3 months ago

Hi everyone,

I'm trying, for 3 days already, to upload multiple photos with Lumen 5.8,

Here is my function in the controller, which is the way I saw in every documentations and tutorial

public function upload(Request $request)
     $file = $request->file('image');
     return response()->json($file->getClientOriginalName());

I expected to get an array of the uploaded files names but I only receive the name of the last file. I tried to upload form Postman and form the Front-End (React) and I have the same result.

Is there something that I didn't understand ? Are there things that have changed in Lumen since then ? Did I forget something ?

I'm stuck in it for days and I really need help!


TorchSK replied 3 months ago

What does



Cameron replied 3 months ago

foreach ($request->file[‘image’] as $file) { // try here ]);

Use this as reference

Also make sure your input is files[] not file

Che k the request by dd($request); that will show you if all images are coming through as an array and if you need to loop over them

Ivan Hervé replied 2 months ago

@TorchSK I get this from Postman

UploadedFile {#95
  -test: false
  -originalName: "img4.jpeg"
  -mimeType: "image/jpeg"
  -error: 0
  #hashName: null
  path: "C:\wamp64\tmp"
  filename: "php46B3.tmp"
  basename: "php46B3.tmp"
  pathname: "C:\wamp64\tmp\php46B3.tmp"
  extension: "tmp"
  realPath: "C:\wamp64\tmp\php46B3.tmp"
  aTime: 2019-04-20 16:09:59
  mTime: 2019-04-20 16:09:59
  cTime: 2019-04-20 16:09:59
  inode: 0
  size: 51102
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "C:\wamp64\tmp\php46B3.tmp"

However, I tried to upload 3 pictures (from Postman still)

Ivan Hervé replied 2 months ago

Hi @Cameron,

dd($request->file[‘image’]) returns null unfortunately

And to make a foreach I have to be sure that $request->file(‘image’) returns an array but it doesn't. It still returns the last one file

Sign in to participate in this thread!

We'd like to thank these amazing companies for supporting us