Wednesday, 26 August 2020

Codeigniter 3 and Ion-Auth application bug: undefined index userfile

I am working on a Social Network application with Codeigniter 3, Ion-Auth and Bootstrap 4. You can see the Github repo HERE.

I have tried to add an avatar at user's registration.

For this purpose, I first added an "avatar" column to the users table. Then, in the view I added:

<div class="form-group">
    <?php $avatar['class'] = 'form-control';
    echo lang('edit_user_avatar_label', 'avatar');?>
    <input type="file" class="form-control" name="userfile" id="avatar" size="20">
</div>

In the Auth controller (application/controllers/Auth.php) I created this upload method:

public function upload_image() {
    $config['upload_path'] = './assets/img/avatars';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $config['max_size'] = 2048;

    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());
        $this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user', $error);
    } else {
        $this->data = array('image_metadata' => $this->upload->data());
        $this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user', $this->data);
    }
}

Finally, to the existing $additional_data array, from the orihinal create_user() method, I added the line 'avatar' => $_FILES['userfile']['name']:

$additional_data = [
    'first_name' => $this->input->post('first_name'),
    'last_name' => $this->input->post('last_name'),
    'avatar' => $_FILES['userfile']['name'],
    'company' => $this->input->post('company'),
    'phone' => $this->input->post('phone'),
];

The above line, when added to the $data array from the edit_user($id) method, has no errors, yet when added to the $additional_data array, it gives the error: Undefined index: userfile.

Where is my mistake?


EDIT:

Using 'avatar' => $this->upload->data('file_name') instead of 'avatar' => $_FILES['userfile']['name'] gives an Undefined property: Auth::$upload error.



from Codeigniter 3 and Ion-Auth application bug: undefined index userfile

No comments:

Post a Comment