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));
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'));
}
@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'));
}
Please or to participate in this conversation.