首页 > 解决方案 > Codeigniter 3 和 Ion-Auth 应用程序错误:未定义的索引用户文件

问题描述

我正在使用 Codeigniter 3、Ion-Auth和 Bootstrap 4 开发社交网络应用程序。您可以在此处查看 Github 存储库

我试图在用户注册时添加头像。

为此,我首先在表格中添加了一个“头像”列users。然后,在我添加的视图中:

<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>

Auth控制器 ( application/controllers/Auth.php) 中,我创建了这个上传方法:

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);
    }
}

最后,对于现有的$additional_data数组,从原始create_user()方法中,我添加了以下行'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'),
];

上面的行,当$dataedit_user($id)方法添加到数组时,没有错误,但是当添加到$additional_data数组时,它给出了错误: Undefined index: userfile

我的错误在哪里?


更新:

我替换<?php echo form_open("auth/create_user");?><?php echo form_open_multipart("auth/create_user");?>.

结果:图像文件名(带扩展名)被添加到users表格avatar列中。但是有一个问题:图像的实际上传./assets/img/avatars并没有发生。

标签: phpcodeigniter

解决方案


终于工作了!!

    if ($this->form_validation->run() === TRUE)
    {
        $email = strtolower($this->input->post('email'));
        $identity = ($identity_column === 'email') ? $email : $this->input->post('identity');
        $password = $this->input->post('password');

        //return $this->upload_image();
        $config['upload_path'] = './assets/img/avatars';
        $config['file_ext_tolower']     = TRUE;
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile'))
        {
                $error = array('error' => $this->upload->display_errors());
                print_r($error);
                $file_name = null;
        }
        else
        {
                $file_name = $this->upload->data('file_name');
        }
        $additional_data = [
            'first_name' => $this->input->post('first_name'),
            'last_name' => $this->input->post('last_name'),
            'avatar' =>  $file_name,
            'company' => $this->input->post('company'),
            'phone' => $this->input->post('phone'),
        ];
        print_r($additional_data);
    }

结果数组

Array ( [first_name] => admin [last_name] => admin [avatar] => design.png [company] => admin [phone] => 9999999999 )

推荐阅读