10

Just wondering why I can't get value of array name from input type file.

HTML:

<input type="file" name="collateral_photo[]" id="collateral_photo" class="default"/>

The name of this input file is an array.

PHP Laravel

return Request::file('collateral_photo');

The return result is [{}]

What I want right now just how to get value from this input file with array name.

Please, please help me out.

Thank you very much for any help.

3
  • 1
    You may have forgot to add enctype="multipart/form-data" to your form. Commented Jun 11, 2015 at 2:39
  • I do input this enctype already. Commented Jun 11, 2015 at 2:47
  • Don't use id use name instead. Commented Apr 11, 2019 at 17:49

3 Answers 3

16

use the below code, since the name attribute is an array

$files = Request::file('collateral_photo');
return $files[0];

or if you want second file

return $files[1];

//if you want to access second file and so on

you need to access the file with array index specified.

If you want to return the whole files array itself then use

return $files
Sign up to request clarification or add additional context in comments.

5 Comments

@Sourabh...If I use this, the return result get nothing. :(
what is the result of var_dump() ?
surly if I return with specified offset like return $files[0], it show me the value. But if I try with return $files, it give me this [{}].
i do not see any reason for it return [{}], can you post the code in your question where you are accessing it? Might be you are accessing it in a wrong manner
Ohhh...I solved it now. I used return with specified offset to loop the value. Thanks very much. :)
4

And going one step further we can work with multiple-level-arrays like so:

<input name="channel[1][file]" type="file">
<input name="channel[2][file]" type="file">

in laravel you would need a structure

$files = $request->file('channel');
$file1 = $files[1]['file'];
$file2 = $files[2]['file'];

1 Comment

It works fine for me! in conjunction with this check function $request->hasFile("channel.1.file")
3

If it is an array of files do:

$request()->file($fieldName)[$key]->getClientOriginalName();

1 Comment

remove "()" on request and working, $request->file($fieldName)[$key]->getClientOriginalName();

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.