/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $current_user = \Auth::user(); $current_user_id = $current_user->id; $username = $current_user->username; $files = Input::file('images'); // Making counting of uploaded images $file_count = count($files); // start count how many uploaded $uploadcount = 0; foreach ($files as $file) { $rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc' $validator = Validator::make(array('file' => $file), $rules); if ($validator->passes()) { $destinationPath = base_path() . '/public/images/users/' . $username . '/pictures'; $filename = $file->getClientOriginalName(); $upload_success = $file->move($destinationPath, $filename); $pictures = UserPhoto::create(array('user_id' => $current_user_id, 'image' => $filename)); $uploadcount++; } } if ($uploadcount == $file_count) { Session::flash('success', 'Upload successfully'); return Redirect::to('profile/photo'); } else { return Redirect::to('profile/photo')->withInput()->withErrors($validator); } /* $filname = \Input::file('uploadpicture')->getClientOriginalName(); $imageName = \Input::file('uploadpicture')->getClientOriginalExtension(); \Input::file('uploadpicture')->move(base_path() . '/public/images/users/'.$username.'/pictures', $filname); $pictures= UserPhoto::create(array( 'user_id' => $current_user_id, 'image' => $filname )); return \Redirect::to('profile/photo'); */ }
/** * Remove the specified resource from storage. * * @param int $id * * @return \Illuminate\Http\Response */ public function destroy($id) { $photo = UserPhoto::findOrFail($id); $photo->delete(); return redirect()->back(); }
<?php /* |-------------------------------------------------------------------------- | Application Routes |-------------------------------------------------------------------------- | | Here is where you can register all of the routes for an application. | It's a breeze. Simply tell Laravel the URIs it should respond to | and give it the controller to call when that URI is requested. | */ Route::get('/', function () { return view('welcome'); }); Route::post('/upload', function () { $userPhoto = new \App\UserPhoto(); $userPhoto->photo = \Input::file('file'); $userPhoto->save(); }); Route::get('/show/{id}', function ($id) { $userPhoto = \App\UserPhoto::find($id); return '<img src="' . $userPhoto->photo->large . '" />'; });