2

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

3 Answers 3

5

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>
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

0

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.