In this tutorial im going to share how to remove old image when doing update the image just follow the below code.
Error:-
local.ERROR: unlink(C:\xampp\htdocs\ds-trainer-ms/storage/app/public/resume/1683885829.pdf): No such file or directory {"userId":368,"email":"rn9r84fKIsn4TPbLU1aefps=","exception":"[object] (ErrorException(code: 0): unlink(C:\\xampp\\htdocs\\ds-trainer-ms/storage/app/public/resume/1683885829.pdf): No such file or directory
Solution :-
public function update(Request $request, AddProfile $addProfile)
{
$profile = AddProfile::find($request->id);
$profile->name = $request->name;
$profile->email = $request->email;
$profile->phone = $request->phone;
if ($request->hasFile('image')) {
$image = $request->file('image');
$new_name = rand() . '.' . $image->getClientOriginalExtension();
$image->move(public_path('images'), $new_name);
// delete old image
if ($profile->image) {
$old_image = public_path('images') . '/' . $profile->image;
if (file_exists($old_image)) {
unlink($old_image);
}
}
$profile->image = $new_name;
}
$profile->save();
return redirect('home')->with('success', 'Profile is successfully updated');
}
Now update and old image unlink working successfully.