如何解决Codeigniter 3和Ion-Auth应用程序错误:未定义的索引用户文件
我正在使用Codeigniter 3, Ion-Auth 和Bootstrap 4开发社交网络应用程序。您可以看到 Github repo HERE 。
我尝试在用户注册时添加头像。
为此,我首先在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
数组时,它给出了错误:{{1 }}。
我的错误在哪里?
更新:
我将Undefined index: userfile
替换为<?php echo form_open("auth/create_user");?>
。
结果:图像文件名(带有扩展名)被添加到<?php echo form_open_multipart("auth/create_user");?>
表users
列中。但是存在一个问题:图像实际上传到avatar
不会发生。
解决方法
努力工作!
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 )
,
更新
OP在评论中发布了a link to the full code。检查出来,问题很明显。我在回答下面的评论中描述了它以及修复方法。在此处复制该评论:
您可以使用
upload_image()
方法加载上载库on line 473。但是,您正在以另一种方法(在$this->upload->data()
中使用line 530来调用create_user()
,在该方法中您尚未加载上载库。将代码从upload_image()
移动到create_user()
。如果需要,请在重构后对其进行重构,直到简化为止
原始答案
您似乎在研究the documentation,您的代码与他们提供的示例非常相似。但是,您还没有完成最后的关键步骤,他们无法解释如何访问上载文件的详细信息! :-)
他们演示了如何通过返回带有上传数据的视图来做到这一点:
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
因此,可以通过$this->upload->data()
而不是PHP的超全局$_FILES
来访问上传文件信息。
文档继续描述the data()
method:
data([$index = NULL])
[...]
这是一个帮助程序方法,它返回一个数组,其中包含与您上传的文件有关的所有数据。
[...]
要从数组中返回一个元素:
$this->upload->data('file_name'); // Returns: mypic.jpg
因此对于您的Ion Auth代码,这应该可以工作(假设文件名就是您需要存储的全部内容):
$additional_data = [
'first_name' => $this->input->post('first_name'),'last_name' => $this->input->post('last_name'),'avatar' => $this->upload->data('file_name'),'company' => $this->input->post('company'),'phone' => $this->input->post('phone'),];
,
问题似乎出在您的表格上。我查看了您的代码,发现auth/create_user.php
使用form_open()
方法而不是form_open_multipart()
方法,因为普通形式不会发布文件,因此在您的控制器中无法获取userfile
索引来自$additional_data
变量。
正如其他答案中已明确解释的那样,这是为您提供的“复制和粘贴”答案。
重申已经说过的话。
$this->upload->data('file_name'),
不存在,因为您没有执行创建它所需的步骤,因此也为什么收到非常“明确说明”的错误消息。
所以您需要添加...
$config['upload_path'] = './assets/img/avatars/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload',$config);
$this->upload->do_upload('userfile');
所以您的代码变成了...
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['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload',$config);
$this->upload->do_upload('userfile');
$additional_data = [
'first_name' => $this->input->post('first_name'),'phone' => $this->input->post('phone'),];
}
现在,由于在do_upload()方法中具有此功能,因此可以将文件上传代码放在另一个方法中,并从两个方法中调用它,因此您不必“重复自己”。我让你自己解决。
更新:可能的重构
创建一种新方法来初始化文件上传
protected function init_do_upload() {
$config['upload_path'] = './assets/img/avatars';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload',$config);
}
您现有的do_upload()变成...
/**
* Upload avatar
*/
public function do_upload() {
$this->init_do_upload();
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form',$error);
} else {
$this->data = array('upload_data' => $this->upload->data());
$this->_render_page('auth' . DIRECTORY_SEPARATOR . 'create_user',$this->data['upload_data']);
}
}
create_user()中的代码段变为...
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');
$this->init_do_upload();
$this->upload->do_upload('userfile');
$additional_data = [
'first_name' => $this->input->post('first_name'),];
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。