artisticre's avatar

Image Delete On Update

I have this code. I tried following the documentation as ya'll mention. It works. It uploads the image and puts it in the storage in the public folder The only problem is when I update the image, its supposed to delete the OldFileName out of the public folder after updating the new image but its not. It adds the new file to the folder but doesn't delete the old one. Any ideas why?

public function updateProfile(Request $request, $id)
    {
        $profile = User::find($id);

        $this->validate($request, array(
            'name' => 'min:3|max:255',
            'adddress' => 'min:3|max:255',
            'city' => 'min:3|max:255',
            'state' => 'min:3|max:255',
            'zipcode' => 'min:3|max:255',
            'mobile'=> 'max:25',
            'image' => 'mimes:jpg,jpeg,png,gif|max:4096',
            'email' => 'email'
           
           ));

           
           $profile->name = $request->input('name');
           $profile->address = $request->input('address');
           $profile->city = $request->input('city');
           $profile->state = $request->input('state');
           $profile->zipcode = $request->input('zipcode');
           $profile->mobile = $request->input('mobile');
           $profile->email = $request->input('email');

           
           if($request->hasFile('image')) {
               $image = $request->file('image');
               $filename = time() .'.'. $image->getClientOriginalExtension();
               $location = public_path('images/profile/'. $filename);
               Image::make($image)->save($location);
               $oldFileName = $profile->image;
               $profile->image = $filename;
               Storage::delete('$oldFileName');
           }
           $profile->save();
           Session::flash('success','Profile Details Updated Successfully');
           return view('employee.profile',compact('profile'));
        }

0 likes
6 replies
Tippin's avatar

Why are you passing the filename as a variable but using single quotes? Also, you need to supply it with the full path. Try:

Storage::delete(public_path('images/profile/'.$oldFileName));
artisticre's avatar

I tried that and it still won't delete the old Image.

Tippin's avatar

@artisticre Could be because you are using the public path, and storage may use the public path by default from storage/app/public as a disk. Maybe try

use Illuminate\Support\Facades\File;

if($request->hasFile('image')) {
   $image = $request->file('image');
   $filename = time() .'.'. $image->getClientOriginalExtension();
   $location = public_path('images/profile/'. $filename);
   Image::make($image)->save($location);
   File::delete(public_path('images/profile/'.$profile->image));
   $profile->image = $filename;
}
$profile->save();
artisticre's avatar

Perfect. This works. Only one question. I have part of my migration it adds a default avatar image when a user registers.

            $table->string('image')->nullable()->default('avatar.jpg')->after('mobile');

so on my profile page I have

                    <img src="{{ asset('images/profile/'.$profile->image) }}" alt="{{$profile->name}}" class="rounded-circle" width="150">

But when I update the image, it deletes the avatar image so when a new user registers it is missing. Where would I put the avatar image so it doesn't get deleted?

Tippin's avatar
Tippin
Best Answer
Level 13

@artisticre Easiest option off the top of my head...just do a simple exact string match if the filename is avatar.jpg

public function updateProfile(Request $request, $id)
{
    $profile = User::find($id);

    $this->validate($request, array(
        'name' => 'min:3|max:255',
        'adddress' => 'min:3|max:255',
        'city' => 'min:3|max:255',
        'state' => 'min:3|max:255',
        'zipcode' => 'min:3|max:255',
        'mobile'=> 'max:25',
        'image' => 'mimes:jpg,jpeg,png,gif|max:4096',
        'email' => 'email'

    ));

    $profile->name = $request->input('name');
    $profile->address = $request->input('address');
    $profile->city = $request->input('city');
    $profile->state = $request->input('state');
    $profile->zipcode = $request->input('zipcode');
    $profile->mobile = $request->input('mobile');
    $profile->email = $request->input('email');

    if ($request->hasFile('image')) {
        $image = $request->file('image');
        $filename = time() .'.'. $image->getClientOriginalExtension();
        $path = public_path('images/profile/');
        Image::make($image)->save($path.$filename);
        if ($profile->image !== 'avatar.jpg') {
            File::delete($path.$profile->image));
        }
        $profile->image = $filename;
    }
    $profile->save();
    Session::flash('success','Profile Details Updated Successfully');
    return view('employee.profile',compact('profile'));
}
artisticre's avatar

Thank you thank you! This was perfect. Thanks again for the assist!

1 like

Please or to participate in this conversation.