Array to string conversion, Laravel Validation

0

I have the following code:

HTML:

<form method="POST" enctype="multipart/form-data" action="/admin/dashboard/category/{{$action}}">
...
    <fieldset class="form-group">
        <label for="image">Imagem</label>
        <input type="file" id="image" name="img">
    </fieldset>

</form>

ControllerPost:

use Validator;

....

$rules = array('img' => 'image|max:1024*1024');
$messages = array(
    'img.image' => 'Só pode ser uma imagem (jpg, gif ou png)',
    'img.max' => 'ficheiro muito pesado... upload máximo é 1 MB'
);

$validator = Validator::make($request->all(), $rules, $messages);

if($validator->fails()) {
    return redirect()->back()->withErrors($validator);
}
else {
    dd('heya');
}

When I upload the file, the following message appears:

  

ErrorException in FileLoader.php line 109: Array to string conversion

Someone why and how to solve?

    
asked by anonymous 26.12.2015 / 19:35

1 answer

0

The exception message itself already tells you what is happening.

It is not possible to convert the array to string because the field "img" is an array.

Every field of type file generates an array in php.

In this way: $ _FILES ['img']

Where you have:

$_FILES['img']['name'] // nome real do arquivo na sua maquina
$_FILES['img']['tmp_name'] // nome temporário do arquivo
$_FILES['img']['size'] //Tamanho do arquivo em bytes
$_FILES['img']['type'] // Tipo / mime extensão do arquivo
$_FILES['img']['error'] // Erros ocorridos na tentativa de envio

And to upload files to laravel, you can do the following:

$path = $request->file('img')->storeAs('pastaDeUpload');

For more information: link

    
24.12.2018 / 16:39