3

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.

Yash Lotan
  • 378
  • 1
  • 5
  • 21

1 Answers1

0

From the documentation, to delete files you should use Storage::delete($filename)

Save your pictures in the storage directory and serve them by creating a route as follows in your 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 your Views, create img tags using the HTML and the URL helper from Laravel:

HTML::image(URL::action('UsersController@profilePicture', array($user['id'])))

Make sure you have the Intervention package installed and you are good to go

clod986
  • 2,527
  • 6
  • 28
  • 52
  • Storage::delete($filename) will delete files from App\storage\app\public\profilepics (which do not exits). But the pictures are stored in App\public\profilepics – Yash Lotan Mar 17 '16 at 12:53
  • Read the full documentation, in the configuration section it is clearly mentioned [documentation](https://laravel.com/docs/5.1/filesystem#configuration) – Yash Lotan Mar 17 '16 at 12:57
  • Why don't you save images in the storage folder? – clod986 Mar 17 '16 at 14:57
  • I tried that too but another problem arised that how to retrieve them because "Storage" will give the contents of jpg stored not the file name. – Yash Lotan Mar 17 '16 at 15:25
  • Also i tried by changing storage path in config/filesystem.php but none works. – Yash Lotan Mar 17 '16 at 16:25
  • I handle it using a route. At the moment I'm working on serving images, but you can check my code here http://stackoverflow.com/questions/36066144/how-should-i-serve-an-image-with-laravel/36066273#36066273 – clod986 Mar 17 '16 at 16:31
  • I liked your approach and I am getting perfect results but how should I pass that url or image to a view (dashboard) – Yash Lotan Mar 17 '16 at 17:39
  • use the built-in url builder URL::action('Controller@action', array($params)) – clod986 Mar 18 '16 at 10:31
  • Can you elaborate, how to use this? – Yash Lotan Mar 18 '16 at 10:49
  • getting a big error `Action App\Http\Controllers\HomeController@profilePicture not defined. (View: /home/yash/website/resources/views/dashboard.blade.php)` I solved it by including profilePicture in routes file – Yash Lotan Mar 18 '16 at 12:35
  • But when I goto dashboard I see blank image and when I saw its source I found this `` which should be `` – Yash Lotan Mar 18 '16 at 12:51
  • I think it depends from your Laravel version. Please refer to this link https://laravel.com/docs/5.2/helpers#method-action – clod986 Mar 18 '16 at 14:27
  • I am using laravel 5.2.22, what version did you use while coding this? – Yash Lotan Mar 18 '16 at 16:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/106747/discussion-between-clod986-and-yash-lotan). – clod986 Mar 18 '16 at 17:00