简体   繁体   中英

Laravel Call to undefined method Intervention\Image\File::delete()

I made a method which allows the user to update their profile picture. I also added a function which deletes their old profile picture if they update to a new one. But i get an error saying Call to undefined method Intervention\\Image\\File::delete() .

What is causing this? In the code below it makes perfect sense for me. I hope you guys can help. Thanks in advance

Code: UserController.php

public function update_avatar(Request $request) {

    $this->middleware('auth');

    if ($request->hasFile('avatar')) {
        $avatar = $request->file('avatar');
        $filename = Auth::user()->username . time() . '.' . $avatar->getClientOriginalExtension();
        Image::make($avatar)->fit(300,300)->save( public_path('/uploads/avatars/' . $filename));

        $user = Auth::user();
        $user->avatar = $filename;
        $user->save();

        //Verwijderd vorige foto
        if ($user->avatar != 'default.jpg') {
            $path = 'uploads/avatars/';
            $lastpath = Auth::user()->Avatarpath;
            File::delete(public_path($path . $lastpath));
        }
    }

    return view('user.profile', array('user' => Auth::user()));

Route: Route::post('{username}/profile', 'UserController@update_avatar');

The form that is supposed to do this.

<div class="row">
        <div class="col l12 m12 s12">
            <div class="card">
                <div class="card-content">
                    <form enctype="multipart/form-data" action="profile" method="POST">
                        <input type="file" name="avatar">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <input type="submit" class="pull-right btn btn-primary" value="Change profile">
                    </form>
                </div>
            </div>
        </div>
    </div>

Changing the picture works just fine. If I reload the page the profile picture is updated but it doesn't delete the old one.

try this. does this help.

 public function update_avatar(Request $request) {

        $this->middleware('auth');

        if ($request->hasFile('avatar')) {
            $avatar = $request->file('avatar');
            $filename = Auth::user()->username . time() . '.' . $avatar->getClientOriginalExtension();
            Image::make($avatar)->fit(300,300)->save( public_path('/uploads/avatars/' . $filename));

            $user = Auth::user();
            $user->avatar = $filename;
            $user->save();

            //Verwijderd vorige foto
            if ($user->avatar != 'default.jpg') {
                 $path = public_path('uploads'.DIRECTORY_SEPARATOR.'avatars'. DIRECTORY_SEPARATOR.Auth::user()->Avatarpath);
                  if (file_exists($path)) {
                    unlink($path);
                  }
            }
        }

        return view('user.profile', array('user' => $user));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM