How to delete an image from the Storage laravel 5.2 folder?

0

I'm using this code to upload image is all working my doubt and in the condition if to delete the old image I have to leave a default.jpg image as default plus my condition if deleting this image. my question and how can I leave this image always default and continue using the deletion condition can anyone help?

 public function update_avatar(Request $request){

    if($request->hasFile('avatar')){
        $user            = Auth::user();
        $old_avatar      = $user->avatar;
        $file            = $request->file('avatar');
        $filename        = time() . '.' . $file->getClientOriginalExtension();
        $old_file_avatar = $old_avatar  . '.' . $file->getClientOriginalExtension();
        $image           = Image::make($file);

        $image->fit(250, 250, function ($constraint) {
            $constraint->aspectRatio();
        });

        Storage::put($filename, (string) $image->encode());

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

    }

    //delete image
    if (Auth::user() !== $filename) {
        Storage::delete($old_avatar);
    }

    return redirect('profile');
}

public function getUserImage()
{
    $user        = Auth::user();
    $old_avatar  = $user->avatar;
    $file        = Storage::disk('local')->get($old_avatar);
    return Response::make($file,200,[ 'Content-Type' => $old_avatar]);


}

I want to delete the old image and put the new one if this already makes it the issue and I do not want it to overwrite the Default.jpg image that is the default in the database for all users.

 //delete image
if (Auth::user() !== $filename) {
    Storage::delete($old_avatar);
}
    
asked by anonymous 21.09.2017 / 20:01

0 answers