php - 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'),
];
上面的行,当$data
从edit_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
并没有发生。
解决方案
终于工作了!!
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 )
推荐阅读
- python - 在python中没有模块的比较运算符的切片列表
- javascript - 如何在比较 JSON 对象的差异时获得 TRUE 或 FALSE 答案
- r - R 在逻辑回归中返回的系数比预期的要多
- java - javac 在尝试了我能找到的所有东西后无法识别(不重复)
- java - Java 多重继承的特殊问题
- javascript - ReactJS 中是否有一个函数可以在加载类中的任何内容之前等待 Redux Action 完成?
- python - 是否可以修补以前创建的模拟调用的方法?
- python - 卷积神经网络中超参数的优化
- mongodb - 从 docker_compose 文件运行时,mongodb 容器错误的权限被拒绝
- javascript - 您有什么想法为什么在这种情况下文本选择突出显示不会消失?