【问题标题】:Undefined index in codeignitercodeigniter 中未定义的索引
【发布时间】:2021-08-09 09:15:15
【问题描述】:

我正在使用 codeignitor 上传文件

但是当我对这些代码进行编码时,会显示 *Undefined index: *...

我做错了什么请给任何建议。

查看:

<form role="form" id="addNotic" action="<?php echo base_url() ?>upload" method="post" >
                        <?php echo form_open_multipart('upload/addNotification');?> 
<input type="file" name="userfile" id="userfile" class="form-control required"  />
<input type="submit" class="btn btn-primary" value="Submit" name="submit"/>
</form>

这是控制器:

function upload(){
            $this->load->library('form_validation');
            $config['upload_path']   = './uploads/'; 
            $config['allowed_types'] = 'gif|jpg|png'; 

            $this->load->helper(array('form', 'url'));

            if($this->upload->do_upload('userfile'))
            {
                $this->addNewNotification();// controler for view
                 $this->session->set_flashdata('success', 'New Notification created successfully');
            
            }
            else
            {
                  $this->addNewNotification();
                 $this->session->set_flashdata('error', 'New Notification not created ');
            }
}

显示错误

【问题讨论】:

  • 写完整的错误信息。
  • 哪个文件/层产生了错误?

标签: php error-handling codeigniter-3 file-handling


【解决方案1】:

我强烈建议您阅读 codeigniter 用户指南。

您的查看代码:

<form role="form" id="addNotic" action="<?php echo base_url() ?>upload" method="post" >
<?php echo form_open_multipart('upload/addNotification');?> 
<input type="file" name="userfile" id="userfile" class="form-control required"  />
<input type="submit" class="btn btn-primary" value="Submit" name="submit"/>
</form>

前两行都在创建&lt;form&gt; 标签。阅读并理解什么 form_open_multipart() 确实如此。 对于这个“答案”的其余部分,我将完全忽略第二行。

看来第一个表单定义正在被提交操作。

<form role="form" id="addNotic" action="<?php echo base_url() ?>upload" method="post" >

请注意,您正在调用名为 Upload 的控制器。当您使用 No Method 声明控制器名称时,默认情况下,它需要一个名为 index 的方法。 (这将是您当前看到的错误的原因)。 IE /upload 将转换为 /upload/index 和 ERROR,因为您没有索引方法!

在您的情况下,您的视图代码将是

<form role="form" id="addNotic" action="<?php echo base_url() ?>upload/upload" method="post" enctype="multipart/form-data">
<input type="file" name="userfile" id="userfile" class="form-control required"  />
<input type="submit" class="btn btn-primary" value="Submit" name="submit"/>
</form>

请注意对 action 的更改以及上面添加的 enctype

或者(使用 CodeIgniter 表单助手 form_open_multipart()

<?php echo form_open_multipart('upload/upload',array('id'=>'addNotic','role'=>'form');?> 
<input type="file" name="userfile" id="userfile" class="form-control required">
<input type="submit" class="btn btn-primary" value="Submit" name="submit">
</form>

我建议您同时尝试并在浏览器中查看生成的 HTML(鼠标右键单击 - 查看源代码。适用于您的浏览器的任何内容)以查看正在创建的内容。

您“可能”想将上传方式更改为“userfile”,使其变为 /upload/userfile,这比上传/上传更有意义,但这取决于您。

【讨论】:

  • 感谢您的回复,是的,我解决了这个问题,现在它的工作......
猜你喜欢
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多