How to delete an image from Storage Folder 5.2?

Asked

Viewed 245 times

0

I’m using this code to upload image this all working my doubt and in the if condition to delete the old image I have to leave a default image.jpg as default plus my condition if this deleting that image. my question and how can I leave this image always default and continue using the condition to delete someone can 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 already makes the question and I do not want it to overwrite the default image.jpg that and default in the database for all users.

 //delete image
if (Auth::user() !== $filename) {
    Storage::delete($old_avatar);
}
  • I don’t understand your question!

  • Vamos la I’m saving my images in the Storage folder and I use the Intervention Image that creates a link in the database of this folder, inside my Storage folder I have an image as default called default.jpg that is loaded as default in the database. the question is that when I upload the image of a user it is replaced. the question is that I cannot do it because it is standard for all users.

1 answer

-1

use the unlink function().

Example:

unlink('caminho_da_imagem');
  • Hi! Can you explain why this line of code solves the problem?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.