Created
February 20, 2018 20:18
-
-
Save poloey/954f547bfe22d917583f29abbe133499 to your computer and use it in GitHub Desktop.
file_upload_in_laravel_with_unlink
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
if ($request->has('avatar')) { | |
$avatar = $request->file('avatar'); | |
$filename = time() . '.' . $avatar->getClientOriginalExtension() ; | |
$relative_file_path = '/images/uploads/' . $filename; | |
$absolute_file_path = public_path($relative_file_path); | |
Image::make($avatar)->resize(350, 350)->save($absolute_file_path); | |
$contact = Contact::find($request->input('contact_id')); | |
$old_image_abs_path = public_path($contact->image); | |
if (file_exists($old_image_abs_path) ) { | |
unlink($old_image_abs_path); | |
} | |
$contact->image = $relative_file_path; | |
$contact->save(); | |
return redirect()->back(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment