Santanu94's avatar

Existing Image is not getting deleted after uploading a new image

use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;
-------
if (request('image')) {
    // delete the old image file
    if (File::exists(public_path('backend/img/categories/main/' . $mainCat->image))) {
        File::delete(public_path('backend/img/categories/main/' . $mainCat->image));
       }

    // save the new image file
    $image = request()->file('image');
    $img = rand() . '.' . $image->getClientOriginalExtension();
    $location = public_path('backend/img/categories/main/' . $img);

    $imageResize = Image::make($image);
    $imageResize->fit(300, 300)->save($location);

    // update the category record with the new image filename
    $mainCat->image = $img;
}
$mainCat->save();

The image is saving but it cant for some reason read my old image and delete it to save new image rather its creating a new image leaving the old image in that folder. Did the same for destroy function and weirdly its working there.

0 likes
6 replies
Snapey's avatar

Your $location seems to be the full path of the image, so thats what is in the database. No need to prefix it with the path again when deleting ?

Santanu94's avatar

@Snapey I found the solution apparently I had to first set $mainCat->image in a variable

$oldImage = $mainCat->image;
if (request()->hasFile('image')) {
    // delete the old image file
    if (File::exists(public_path('backend/img/categories/main/' . $oldImage))) {
        File::delete(public_path('backend/img/categories/main/' . $oldImage));
    }
}

that did the work...

Snapey's avatar

@Santanu94 That should not make any difference. Make sure it still works as expected.

Santanu94's avatar
Santanu94
OP
Best Answer
Level 3

@Snapey Honestly even I don't know what is going on but these are the changes I made

public function update(Category $category)
    {
        $mainCat = $category;
        $oldImage = $mainCat->image;
	    $mainCat->update($this->validateCategory($mainCat->id));
        if (request()->hasFile('image')) {
            //Delete the old image file
            if (Storage::disk('mainCat')->exists($oldImage)) {
                Storage::disk('mainCat')->delete($oldImage);
            }

            //Save the new image file
            $image = request()->file('image');
            $img = rand() . '.' . $image->getClientOriginalExtension();
            $location = Storage::disk('mainCat')->path($img);

            $imageResize = Image::make($image);
            $imageResize->fit(300, 300)->save($location);

            //Update the category record with the new image filename
            $mainCat->image = $img;
		}

        $mainCat->slug = Str::slug(request('slug'));
        $mainCat->save();
}

even if I put $oldImage after $mainCat->update it doesnt work.

SDCODE's avatar

Your code looks correct, but it's possible that the file deletion is failing for some reason. One thing you can do is to add some logging to help debug the issue. Here's an updated version of your code with some logging added:

use Illuminate\Support\Facades\File;
use Intervention\Image\Facades\Image;

if (request('image')) {
    // delete the old image file
    $imagePath = public_path('backend/img/categories/main/' . $mainCat->image);
    if (File::exists($imagePath)) {
        File::delete($imagePath);
        \Log::info('Deleted old image file: ' . $imagePath);
    } else {
        \Log::info('Old image file not found: ' . $imagePath);
    }

    // save the new image file
    $image = request()->file('image');
    $img = rand() . '.' . $image->getClientOriginalExtension();
    $location = public_path('backend/img/categories/main/' . $img);

    $imageResize = Image::make($image);
    $imageResize->fit(300, 300)->save($location);

    // update the category record with the new image filename
    $mainCat->image = $img;
    $mainCat->save();

    \Log::info('Saved new image file: ' . $location);
}

This code will write log messages to the default Laravel log file (storage/logs/laravel.log) whenever it deletes the old image file or saves the new image file. You can use these log messages to help debug the issue. For example, if you see a message indicating that the old image file was not found, then the issue may be that the $mainCat->image property is not set correctly.

1 like

Please or to participate in this conversation.