Search code examples
laravelintervention

Laravel file upload: not detecting a file


When trying to upload an image to the database I get the error:

Call to a member function getClientOriginalExtension() on null

This is my form in blade view

<form data-parsley-validate action="{{ route('kunstenaars.store') }}" method="post" enctype="multipart/form">
    @csrf
  
    <div class="form-row">
        <div class="col form-group">
                <label for="foto">Foto van kunstenaar:</label>
                <input name="foto" id="foto" class="form-control foto" type="file"  accept=".jpg, .jpeg, .png, .svg, .ico"> 
        </div> 
    </div>

    <button type="submit" class="btn btn-lg btn-block btn-primary">Aanmaken</button>

</form>

In my controller:

$kunstenaar = new Kunstenaar();

$avatar = $request->file('foto');
$extension = $avatar->extension();
$filename = time() . '.' . $extension;
$location = public_path('/images/kunstenaars/' . $filename);
Image::make($avatar)->resize(800, 400)->save($location);

I'm using Intervention Image. If i try it without the extension i get this

Intervention\Image\Exception\NotReadableException: Image source not readable


Solution

  • The enctype attribute should be multipart/form-data

    So update the form element with the following

    enctype='multipart/form-data'

    BTW you should implement validation logic before submitting the form request.