F
F
Flasher2016-02-14 19:37:14
Laravel
Flasher, 2016-02-14 19:37:14

Form multiple, if you leave the field empty on submit, then an error occurs, who will tell you why?

There is a form:

<form action="" method="post" enctype="multipart/form-data">
    <input type="file" name="cover[]" multiple/>
    <input type="submit" value="Отправить">
</form>

In the controller: I want to check if the field is empty, then I catch an exception:
$files = $request->file('cover');
Argument 1 passed to Illuminate\Http\UploadedFile::createFromBase() must be an instance of Symfony\Component\HttpFoundation\File\UploadedFile, null given, called in C:\webserver\data\htdocs\topnews\vendor\laravel\framework \src\Illuminate\Http\Request.php on line 415 and defined

If I try to check: it still gives the error
if ($request->hasFile('cover'))

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
Flasher, 2016-02-15
@Flasher

Solution: roll back to version 5.1.*

A
Andrzej Wielski, 2016-02-14
@wielski

if($request->hasFile('cover')){
 //тут обрабатывай файл
}

If there are many files, the name of the input should still be cover (and not cover[]).
Handling multiple files in Laravel:
if($request->hasFile('cover')){
  foreach($request->file('cover') as $file){
    // $file
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question