kendrick's avatar

Avatar Upload / Store Fail

Hello everybody. As I am implementing an Avatar Upload Functionality to my User Profile Setup, I face some problems.

Well, the problem is that when I update my setup with the uploaded avatar, the file name will upload within the DB but doesn't store within my public path.

This is my postEdit Method: $this->validate($request, [

        'first_name' => 'alpha|max:50',
        'last_name' => 'alpha|max:50',
        'location' => 'max:20',
        'status' => 'max:50',
        'avatar' => 'max:255',

    ]);

    
    if($request->hasFile('avatar')) {
        
        $file = Input::file ('avatar');
        $file = $file -> move(public_path().'/uploads/avatars/', time() .'-'. $file->getClientOriginalName());
                     
    }  


    Auth::user()->update([
        'first_name' => $request->input('first_name'),
        'last_name' => $request->input('last_name'),
        'location' => $request->input('location'),
        'status' => $request->input('status'),
        'avatar' => $request->input('avatar'),
    ]);

Any suggestions? Would be very helpful.

0 likes
4 replies
kendrick's avatar

if($request->hasFile('avatar')) {

        $path = $request->avatar->store('avatars', 'public');

        $file = Input::file ('avatar');

        $file = $file -> move(public_path().'/uploads/avatars/', time() .'-'. $file->getClientOriginalName());
        
                     
    }  


    Auth::user()->update([
        'first_name' => $request->input('first_name'),
        'last_name' => $request->input('last_name'),
        'location' => $request->input('location'),
        'status' => $request->input('status'),
        'avatar' => $path,
    ]);

Now spits an Error: undefined variable path

kendrick's avatar

$image = $request->file('avatar'); $filename = time() . '.' . $image->getClientOriginalExtension(); // $path = storage_path('/uploads/avatars/'. $user->username . $filename ); // Storage::makeDirectory($path, 0777, true); $location = public_path('uploads/avatars/' . $filename); Image::make($image)->resize(300,300)->save($location);

$user->avatar = $filename;

kendrick's avatar

Also tried this with the Intervention Package. But it still doesn't stores the image anywhere but in the DB.

By the way, this is my route:

Route::post('profile.edit', [ 'uses' => '\Modernoo\Http\Controllers\ProfileController@postEdit', 'middleware' => ['auth'], ]);

Please or to participate in this conversation.