yudy's avatar
Level 1

How to handle update on image upload

Hi all i new to laravel

recently i try to practice with laravel 5.5 and create simple upload image task

here is my controller on store

        $input = $request->all();

 
        $this->validate($request, [ 
            'photo' => 'sometimes|image|mimes:jpeg,png,jpg,gif,svg|max:500',
        ]);
        


        if ($request->hasFile('photo')) {
            $photo = $request->file('photo');
            $ext = $photo->getClientOriginalExtension();

            if ($request->file('photo')->isValid()) {
                $photo_name = date('YmdHis'). ".$ext";
                $photoName = time().'.'.$request->photo->getClientOriginalExtension();
                $request->photo->move(public_path('photo-upload'), $photoName);
                $upload_path = public_path('photo-upload');
                $input['photo'] = $photoName;
            }
        }

contoller on update


        if ($request->hasFile('photo')) {
            // delete the old/previous one
            $exist = Storage::disk('photo')->exists($student->photo);
            if (isset($student->photo) && $exist) {
                $delete = Storage::disk('photo')->delete($siswa->photo);
            }

            // upload new one
            $photo = $request->file('photo');
            $ext = $photo->getClientOriginalExtension();
            $photoName = time().'.'.$request->foto->getClientOriginalExtension();
            if ($request->file('photo')->isValid()) {
                $upload_path = 'photo-upload';
                $request->file('photo')->move($upload_path, $phptpName);
                $input['photo'] = $iphotoName;
            }
        }

the store method works perfectly, it uploaded image to the right folder then update the databse

the problem is on the update, it update the database but it didn't upload the image to the photo-upload folder

Any suggestion?

Thanks

0 likes
1 reply
Jeroen's avatar

I think there's a typo in $phptpName

Please or to participate in this conversation.