mercredi 6 septembre 2017

How to store avatar correctly

(in developer mode) The way I have my Controller now it stores the new image path to the database but doesn't upload it to the 'uploads/avatars' folder.

Also when I try to just edit the profile without uploading a new avatar, it throws an ErrorException undefined variable:avatar,

and the version i have on my hosting server also throws the Errorexception if not uploading but only editing the profile,

And if I try to upload a new avatar it tells me

NotWritableException
Can't write image data to path (/home/http://ift.tt/2f3ls5H)

Anybody knows how to go about fixing this?

My Controller:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Session;
use Image;
use Illuminate\Support\Facades\File;
use App\User;
use DB;


class UserController extends Controller
{

        public function __construct()
   {
        $this->middleware('auth', ['except' => ['index', 'show']]);
  }


  //
     public function index(){
        return view('profiles.profile', array('user' => Auth::user()) );
  }

   public function edit()

{

return view('profiles.edit')->with('info', Auth::user()->profile);

}
     public function update(Request $request)

  {
     $this->validate($request, [
      'location' => 'required',
      'about' => 'required|max:355',
      'passion' => 'required|max:355'
      ]);

     Auth::user()->profile()->update([
     'location' => $request->location,
     'about' => $request->about,
      'passion' => $request->passion

 ]);

 $user = User::find(Auth::user()->id);

// Handle the user upload of avatar

if ($request->hasFile('avatar')) {
    $avatar = $request->file('avatar');
 $filename = time() . '.' . $avatar->getClientOriginalExtension();

  }

    // Delete current image before uploading new image

 if ($user->avatar !== 'man.png' && $user->avatar !== 'woman.png')
    {
     $file = public_path('/uploads/avatars/' . $user->avatar);


  if (File::exists($file)) {
             unlink($file);
     }
  }

Image::make($avatar->getRealPath())->resize(350, 350)->save( public_path('/uploads/avatars/' . $filename ) );

       $user = Auth::user();
       $user->avatar = $filename;
       $user->save();


return back()->with('msg', 'Profiel is bijgewerkt');



    }


}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire