Codeigniter 3和Ion-Auth应用程序错误:未定义的索引用户文件

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-