Error uploading Images with Laravel

Asked

Viewed 1,378 times

0

I’m trying to use Jasny’s Bootstrap file upload(http://www.jasny.net/bootstrap/javascript/#fileinput) to upload photos and save the photo name in the BD with Laravel 5.1 but not working.

Apparently the Rover is capturing the photo when I give the command dd(Request::capture()); it brings the data as per the image below.

inserir a descrição da imagem aqui

But he’s showing the following error:

inserir a descrição da imagem aqui

That would be on that Line

 $extension = $file->getClientOriginalExtension(); // getting image extension

Follows the code

View

 <form method="post"  class="form-group" role="form" id="form"    action=".../perfil/update" enctype="multipart/form-data">

<div class="form-group">
<label>Imagem de Perfil &nbsp;</label>
 <div class="fileinput fileinput-new" data-provides="fileinput">
 <div class="fileinput-new thumbnail">
  <img src="assets/upload/avatar/avatar.png" alt="">
 </div>
 <div class="fileinput-preview fileinput-exists thumbnail"></div>
 <div class="user-edit-image-buttons">
 <span class="btn btn-azure btn-file">
<span class="fileinput-new"><i class="fa fa-picture"></i> Selecione uma imagem</span>
<span class="fileinput-exists"><i class="fa fa-picture"></i>Trocar</span>
<input type="file" id="file" name="foto"> </span>
<a href="#" class="btn fileinput-exists btn-red" data-dismiss="fileinput">
<i class="fa fa-times"></i> Cancelar</a>
</div>
</div>
</div>

Controller

 public function update()
    {

        $id = $this->request->get('id');
        $dadosForm = $this->request->except('_token');
        //
        //Recebe o Arquivo do Form
        $file = $this->request->file('foto');



        if($this->request->hasFile('foto') && $file->isValid()) {


            $destinationPath = '/upload/profile/';
            $extension = $file->getClientOriginalExtension(); // getting image extension
            $fileName = rand(11111,99999).'.'.$extension; // renameing image
            $file->move($destinationPath, $fileName); // uploading file to given path


        }



        if (isset($fileName)) $dadosForm = $this->associado->user_pic = $fileName;

        $this->associado->where('id', $id)->update($dadosForm);

    }
  • Give a dd of $file before the if

  • It brings the image Uploadedfile {#29 -test: false -originalName: "Koala.jpg" -mimetype: "image/jpeg" -size: 780831 -error: 0 }

  • 3

    I am voting to close this question as out of scope because it is that reason that was discussed in the meta, where the question is a typo or some confusion on the part of the AP (like a syntax error or put the wrong index in the array)which will hardly help anyone, as it is a very particular problem.

  • Good @Wallacemaxters I find this totally unnecessary because up there we have a fully functional Upload could be helping several people who are looking for a similar solution. I can even delete the post but would be selfish the idea here is to help and not disqualify doubt from other people because tomorrow who may be in need of help can be you

  • 1

    @BJJ I’m just talking about the direction the answer has taken, it has nothing to do with your code or your question. The title leads us to understand that there is an "error with the image when uploading", but its answer shows that the problem was only a typo or a simple error of use of the array index. Don’t take it personally, I’m just acting on the site’s recommendations

  • There’s even this option at closing time, and I didn’t even need to create a new one. I hesitate, man, closure of website questions already have this option ready for such cases.

Show 1 more comment

1 answer

0


Problem solved

The problem was in the Lines:

 $destinationPath = '/upload/profile/';
 if (isset($fileName)) $dadosForm = $this->associado->user_pic = $fileName;

I switched to:

$destinationPath = 'assets/upload/profile/';
if (isset($fileName)) $dadosForm['user_pic'] = $fileName;

Browser other questions tagged

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