I have a website satoshirps.site
When a new user registers, his profile photo remains empty. Then goto Upload picture to upload a profile picture.
After returning back to dashboard (www.satoshirps.site/dashboard) the user see his uploaded profile picture. Now goto delete picture to delete the current profile picture and then upload an another profile picture using upload.
This time returning to dashboard gives the old picture(which is weird).
When I tried this thing, I uploaded the picture successfully for the second time (I checked the file in my app's folder). Then I was not getting the correct image. Does lavarel have cache or this is a cookie problem. I tried again by deleting browser's cache and cookie but 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 a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the 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 back with error message.
Session::flash('error', 'Uploaded file is 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 is not deleted";
} else {
echo "The image is deleted";
}
}
}
And the 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 you visit dashboard after deleting the file you will see your old profile picture
Please some will please try this and help me.