pandiyan's avatar

File upload in laravel.

Hi ,i need to upload profile picture with using slim.min.css i have file when i check in jquery. but in controller f($request->hasFile('photo')) i got false.

0 likes
7 replies
Nakov's avatar

Are you maybe missing the enctype='multipart/form-data' on your form element in the view? Or on the input file element the name attribute with a value of photo.

pandiyan's avatar

{{Form::open(['route'=> $driverFormRouteParams,'method'=>'POST' ,'id' => 'add_product_form', 'files' => true ])}}

                <input type="hidden" name="img_edit" id="img_edit">
                                <div id="avatar-view" class="avatar">
                                    <div class="slim"
                                        data-push="true"
                                        data-ratio="1:1"
                                        data-label="Drop your image"
                                        data-size="360,360"
                                        data-instant-edit="true"
                                        data-will-remove="imageWillBeRemoved"
                                        data-max-file-size="5"
                                        data-status-file-size="File is too big, maximum file size:  MB."  >
                                            <img src="{{ isset($user) && $user->photo? URL::to('general/img/drivers/' . $user->photo) : URL::to('backEnd/assets/images/placeholder.png') }}" alt="">
                                        <input type="file" name="photo[]" id="upload" >
                                    </div>
                                </div>
{{ Form::close() }}

This my code in view ,i have used .

Nakov's avatar

@pandiyan so just inspect the HTML and make sure that it has the attribute I gave you above, or just add 'enctype' => 'multipart/form-data' in your form open as well.

Nakov's avatar

@pandiyan then what does dd($request->file('photo'));results in, is it null?

Snapey's avatar

You don't get an instance of an uploaded file when using Slim. The image is sent in a regular input field.

This is straight from one of my projects that used Slim

ImageManager is Intervention image, and means you don't need to use the Slim php library

    private function saveProfileImage($request, $member)
    {
        if(!isset($request->photo)) {
            if($request->photoDeleted==1){
                $member->deleteProfileImage();
            }
            return;
        }

        $slim = json_decode($request->photo);

        $img = $this->imageManager->make($slim->output->image);
        $img->save(storage_path() . '/app/public/cropped/photo-' . $member->legacyid . '.jpg');

        $img->resize(150,200)
                ->resizeCanvas(170,220,'center')
                ->resizeCanvas(0,5,'top',true)
                ->text($member->fullname,10,220, function ($font) {
                    $font->file(2);
                    $font->size(16);
                })
                ->save(storage_path() . '/app/public/bordered/photo-' . $member->legacyid . '.jpg');

        return;
    }

There is more going off here than you need since I add a border to the image and put the person's name in the border using Intervention Image

Please or to participate in this conversation.