【问题标题】:Image upload using codeigniter error使用codeigniter错误上传图片
【发布时间】:2019-01-02 04:01:14
【问题描述】:

我正在尝试在代码点火器中制作图像上传表单,但出现以下错误。

严重性:通知

消息:未定义索引:image_file

文件名:controllers/men.php

行号:23

回溯:

文件: C:\xampp\htdocs\CodeIgniter-3.1.8\application\controllers\men.php 行:23 函数:_error_handler

文件:C:\xampp\htdocs\CodeIgniter-3.1.8\index.php 行:315 功能: 需要一次

这是我的视图、控制器和模型代码

1) 查看

<!DOCTYPE html>
<html>
    <head>
        <title>image</title>
        <link rel="stylesheet" type="text/css" href="<?php echo base_url();?>/public/css/bootstrap.css">
    </head>
    <body>
        <form method="post" action="<?php echo base_url()?>men/image_upload" id="upload_form" />
            <input type="file" name="image_file">
            <input type="submit" id="upload" name="upload" value="Upload" />
        </form>
    </body>
</html>

2) 控制器

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Men extends CI_Controller
{
    public function index(){
        $this->load->view("imageupload");
    }

    public function image_upload(){
        $config['upload_path']='./uploads';
        $config['allowed_types']='*';
        $this->load->library('upload',$config);
        $this->upload->do_upload('image_file');
        $image_file = $this->upload->data();
        $data=array('image_file'=> $image_file['image_file']);
        $this->load->model("mymodel");
        $this->mymodel->imagedone($data);   
    }
}
?>

3) 型号

<?php
class Mymodel extends CI_Model{

    function imagedone($data){
        $query = $this->db->insert("image_tbl",$data);
        if($query)
        {
            echo "File is uploaded";
        }
        else{
            echo "failure";
        }
    } 
}

【问题讨论】:

    标签: php mysql


    【解决方案1】:

    这个错误可能是由于form标签的属性缺失造成的,在form标签中添加-> enctype="multipart/form-data" 属性试试:

    <form method="post" action="<?php echo base_url()?>men/image_upload" id="upload_form" enctype="multipart/form-data" /> <input type="file" name="image_file"> <input type="submit" id="upload" name="upload" value="Upload" /> </form>

    【讨论】:

      【解决方案2】:

      首先,如果你想上传文件,你需要在表单中添加 enctype 属性。 将 enctype= multipart/form-data 属性添加到表单中,然后重试。

      <!DOCTYPE html>
      <html>
      <head>
      <title>image</title>
      <link rel="stylesheet" type="text/css" href="<?php echo base_url();? 
       >/public/css/bootstrap.css">
      </head>
        <body>
        <form method="post" enctype="multipart/form-data" action="<?php echo base_url()?>men/image_upload"  
       id="upload_form" />
          <input type="file" name="image_file">
          <input type="submit" id="upload" name="upload" value="Upload" />
      </form>
      
       </body>
        </html>
      

      【讨论】:

      • 我在第一种形式标签中放置了这个斜杠“\”,这可能是它没有进一步工作的原因
      【解决方案3】:

      在您的表单中使用此属性 enctype="multipart/form-data"

      【讨论】:

        猜你喜欢
        • 2019-02-02
        • 2017-10-04
        • 2013-01-11
        • 1970-01-01
        • 2018-04-03
        • 2017-05-28
        • 1970-01-01
        • 2013-12-31
        • 2019-04-27
        相关资源
        最近更新 更多