Skip to content

Instantly share code, notes, and snippets.

@rslahmed
Created May 15, 2020 20:47
Show Gist options
  • Save rslahmed/60daebc3bc84507e69f4283fdb870a30 to your computer and use it in GitHub Desktop.
Save rslahmed/60daebc3bc84507e69f4283fdb870a30 to your computer and use it in GitHub Desktop.
Upload image with laravel
//insert image
$file = $request->image;
$path = '/backend/upload/';
$image = $path.uniqid().time().'.'.$file->getClientOriginalExtension();
$file->move(public_path().($path), $image);
$data['image'] = $image;
//update image
$file = $request->file('image');
if ($file){
$request->validate([
'image' => 'image|mimes:jpg,png,jpeg',
]);
$oldImage = Profile::find($userId)->image;
if ($oldImage){
unlink(public_path().$oldImage);
}
$path = '/uploads/profile/';
$image = $path.time().'.'.$file->getClientOriginalExtension();
$data['image'] = $image;
$file->move(public_path().($path), $image);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment