2

When I select two files, this code doesn't work. I don't know what I'm doing wrong..

This is my controller.

public function createAction()
{
  $router = $this->get('router');
  $request = $this->get('request');
  $description = $request->request->get('description');                                   
  $picturesFromPage = $request->files->all();                                             
  $jsonErrorCreator = $this->get('project_api.create_error_json');
  $nbPictures = count($picturesFromPage);                                                   

  return ($jsonErrorCreator->createErrorJson(101, $nbPictures));
}

In my twig:

<input name="pictures" type='file' multiple='multiple' required>

The Json answers:

{"code":101,"msg":"0 files uploaded","data":1}
6
  • Not an answer, but you access Request object like that: createAction(Request $request) Commented Jun 9, 2014 at 13:35
  • If you var_dump count($picturesFromPage) what does it show? Commented Jun 9, 2014 at 14:15
  • it shows 1 for 2 selected items Commented Jun 9, 2014 at 15:24
  • 1
    @Gura I would recommend to set $picturesFromPage = $request->files->get('pictures'); Also you need to put the name of file input in brackets if you want to upload multiple like name="pictures[]" Commented Jun 9, 2014 at 15:57
  • @Javad it works! So why $request->files->all() don't do this job? Commented Jun 9, 2014 at 16:03

1 Answer 1

3

The files->all() method returns all form parameters not the submitted value(s). It's better to use files->get() instead so that it will get the value(s) of the parameter you want.
Change it as:

$picturesFromPage = $request->files->get('pictures');

Furthermore, if you want to submit an array of values in your form you need to put their name in bracket as

<input name="pictures[]" type='file' multiple='multiple' required>
Sign up to request clarification or add additional context in comments.

Comments

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.