File::delete($path); use in a controller

Submitted by blackrain - 10 years ago

This stumped me for some time and after digging around I found it works great, the uploaded file can be removed from the server as well as the database entry. works well for file sharing and image sharing sites.

public function destroy($id)
    {
		$file = Upload::find($id);
		$filename = Input::get('database field name for file');
		$path = public_path().'/path/to/file/';

		if (!File::delete($path.$filename))
		  {
		  	Session::flash('flash_message', 'ERROR deleted the File!');
		  	return Redirect::to('page name');
		  }
		else
		  {
		  	$file->delete();
			Session::flash('flash_message', 'Successfully deleted the File!');
			return Redirect::to('page name');
		  }
		
	}