Array to string Conversion, Orange Validation

Asked

Viewed 1,626 times

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?

  • widthErrors or withErrors? Let’s start doing this little fix.

  • What is the 109 line? the error basically says, that you tried to manipulate an array with a scalar function.

1 answer

1


The exception’s own message already tells you what’s going on.

Cannot convert array to string because the "img" field is an array.

Every file field generates an array in php.

Thus: $_FILES['img']

Where do 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 do file uploads in the Windows, you can do the following:

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

For more information: https://laravel.com/docs/5.7/filesystem#file-uploads

Browser other questions tagged

You are not signed in. Login or sign up in order to post.