Reputation: 285
In Symfony 2.8 I've got an action which will get always one file in the request. Of course I can handle the situation, when there's no file uploaded, but if there will be more files in one request (although I don't see the possibility without tampering request), I'm interested only in the first one.
Currently I use this code:
request = $this->get('request');
$file = $request->files->all()['files']['0'];
$file->move(...
Is there any better, "prettier" way to do it? I think I can't build form Type for that, because the request comes from the Blueimp jQuery multiupload.
Upvotes: 2
Views: 4112
Reputation: 17759
You could use a default value of an empty array and then use array shift to get the first element like...
/** @var UploadedFile|null $file */
$file = array_shift($request->files->get('files', []));
With this you will either get the first UploadedFile or a null result (if an error occurred or when the array is empty, by default).
Upvotes: 2