php - Laravel behaving madly - showing wrong images -
i have website satoshirps.site
when new user registers, profile photo remains empty. goto upload picture upload profile picture.
after returning dashboard (www.satoshirps.site/dashboard) user see uploaded profile picture. goto delete picture delete current profile picture , upload profile picture using upload.
this time returning dashboard gives old picture(which weird).
when tried thing, uploaded picture second time (i checked file in app's folder). not getting correct image. lavarel have cache or cookie problem. tried again deleting browser's cache , cookie nothing worked.
the upload page :
<!doctype html> <html> <head> <meta charset="utf-8"> <title>upload</title> </head> <body> @if(session::has('success')) {{ session::get('success') }} @endif @if(session::has('error')) {{ session::get('error') }} @endif {{ form::open(array('url' => 'upload', 'files' => true, 'method' => 'post')) }} {!! csrf_field() !!} {{ form::file('image') }} {{ form::submit('upload') }} {{ form::close() }} </body> </html> the controller
<?php namespace app\http\controllers; use app\http\requests; use illuminate\http\request; use illuminate\support\facades\auth; use input; use session; use redirect; use image; use storage; use app\user; use file; class homecontroller extends controller { /** * create new controller instance. * * @return void */ public function __construct() { $this->middleware('auth'); } /** * show application dashboard. * * @return \illuminate\http\response */ public function index() { return view('home'); } public function dashboard() { $user = auth::user(); return view('dashboard', compact('user')); } public function upload() { $user = auth::user(); return view('upload', compact('user')); } public function uploadsave(request $request) { $user = auth::user(); if (input::file('image')->isvalid()) { $filename = 'profilepics/'.$user->username.'.jpg'; file::delete($filename); $img = image::make($_files['image']['tmp_name'])->encode('jpg',75)->resize(200,200)->save('profilepics/'.$user->username.'.jpg'); $dbuser = user::where('id', $user->id)->first(); $dbuser->photo_path = '/profilepics/'.$user->username.'.jpg'; $dbuser->save(); //update user's profile picture location session::flash('success', 'upload successfull'); return redirect::to('upload'); } else { // sending error message. session::flash('error', 'uploaded file not valid'); return redirect::to('upload'); } } public function deletephoto() { $user = auth::user(); $filename = 'profilepics/'.$user->username.'.jpg'; file::delete($filename); if (file_exists($filename)) { echo "the image not deleted"; } else { echo "the image deleted"; } } } and routes file
<?php route::get('/', function () { return view('welcome'); }); route::group(['middleware' => 'web'], function () { route::auth(); route::get('/home', 'homecontroller@index'); route::get('/dashboard', 'homecontroller@dashboard'); route::get('/upload', 'homecontroller@upload'); route::post('/upload', 'homecontroller@uploadsave'); route::get('delete', 'homecontroller@deletephoto'); }); also if visit dashboard after deleting file see old profile picture
please please try , me.
from the documentation, delete files should use storage::delete($filename)
save pictures in storage directory , serve them creating route follows in userscontroller:
public function profilepicture($user){ $profile_picture_url = storage_path().'/profile_pictures/'.$user['id'].'/profile.jpg'; if(!file::exists( $profile_picture_url )) app::abort(404); return image::make($profile_picture_url)->response('jpg'); } then, in views, create img tags using html , url helper laravel:
html::image(url::action('userscontroller@profilepicture', array($user['id']))) make sure have intervention package installed , go
Comments
Post a Comment