omrakhurs's avatar

Cannot upload image file using Form Facade in 5.2

I have been struggling to upload an image in a simple form. Every time I upload the file, it says there is no file, as the functionality I've set up in the controller. Here is the code so far:

The blade form:

 <div class="text-align-center">
    {{Form::open(['url' => 'profile', 'files' => true]) }}
    {{Form::file('avatar')}}
    {{Form::submit('Update',['class' => 'pull-left btn btn-sm btn-primary']) }}
    {{Form::close() }}
</div>

The routes file:

Route::get('profile', 'UserController@profile');
Route::post('profile', 'UserController@avatar');
Route::resource('users','UserController');

The UserController:

public function profile(){

            return view('profile', array('user' => Auth::user()));
    }   

    public function avatar(Request $request){

        if($request->hasFile('avatar')){
                var_dump($request);
        }
        else
                die('there is no file here!');
}

The User model:

protected $fillable = [
        'username', 'email', 'avatar'
    ];

My php.ini has maximum file size limits of 8M, the file I'm trying to upload is only a few KB.

0 likes
6 replies
tomopongrac's avatar

Try

    {!!Form::open(['url' => 'profile', 'files' => true]) !!}
    {!!Form::file('avatar')!!}
    {!!Form::submit('Update',['class' => 'pull-left btn btn-sm btn-primary']) !!}
    {!!Form::close() !!}
</div>

tomopongrac's avatar

Try this dd:

    public function avatar(Request $request){

    var_dump($request);
        if($request->hasFile('avatar')){
                var_dump('there is file');
        }
        else
                die('there is no file here!');
}
omrakhurs's avatar

It produces an infinite loop of null arrays. Then the browser crashes. From what I am able to read, it is going through all the routes.

Please or to participate in this conversation.