Aslam H
Aslam H

Reputation: 1801

Image upload not work laravel 5.4 doesn't get any error

I don't know where do I doing wrong the file not uploaded and the name not store in database.

Here's my controller

      if(Input::hasFile('photo')) {
        $fotoName = 'peg' . $employee->id . '.' .
        $request->file('photo')->getClientOriginalExtension();
        $request->file('photo')->move(
        base_path() . '/public/images/employee/', $fotoName
        );
        $img = Image::make(base_path() . '/public/images/employee/' . $fotoName);
        $img->resize(150, null, function ($constraint) {
          $constraint->aspectRatio();
        });
        $img->save();
        $employee_fotos = Karyawan::find($employee->id);
        $employee_fotos->photo = $fotoName;
        $employee_fotos->save();
      }

Views

<form class="form-horizontal" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
        {{ csrf_field() }}
    <div class="row">
      <!-- left column -->
      <div class="col-md-3">
        <div class="text-center">
          <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
          <h6>Upload a different photo...</h6>

          <input type="file" name="photo" class="form-control" />
        </div>
      </div>
</form>

and I dont get any error and if I make validate its always get please add image or make sure the file extention .jpg etc, for sure I have choice a right image and extention

Upvotes: 2

Views: 6545

Answers (3)

Erik Campobadal
Erik Campobadal

Reputation: 917

add enctype="multipart/form-data" to your form tag:

<form class="form-horizontal" enctype="multipart/form-data" role="form" action="{{ route("karyawan.store") }}" method="post" multiple>
        {{ csrf_field() }}
    <div class="row">
      <!-- left column -->
      <div class="col-md-3">
        <div class="text-center">
          <img src="//placehold.it/100" class="avatar img-circle" alt="avatar">
          <h6>Upload a different photo...</h6>

          <input type="file" name="photo" class="form-control" />
        </div>
      </div>
</form>

Upvotes: 5

surge10
surge10

Reputation: 642

Image Upload not working all round, the problem is the browser is just not giving the full file path for the server to upload.

To verify you can do an alert on the value of the input type to see what I mean

<input type="file" name="photo" onchange="alert(this.value)" class="form-control" />

Go over to How to resolve the C:\fakepath? for the full solution to the problem

read the answer from 'chakroun yesser'

Upvotes: 0

Willie Mwewa
Willie Mwewa

Reputation: 361

public function store(Request $request) 
{

    //dd(request()->all());

    $this->validate(request(), [
        'file'              => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:6000',

    ]);



    $imageName = time().'.'.$request->file->getClientOriginalExtension();
    $request->file->move(public_path('/images/employee/'), $imageName);

}

This should work for you but it will store image with a unix timestamp name. If you use the employer id to store images you might have duplicate images in your storage folder next time you upload a new image with the same employee account. Also add enctype="multipart/form-data" to your form attributes.

Upvotes: 0

Related Questions