简体   繁体   English

使用Codeigniter错误上传图片

[英]Image upload using codeigniter error

i am trying to make an image upload form in code igniter i am getting the following error. 我正在尝试在代码点火器中制作图像上传表单,但出现以下错误。

Severity: Notice 严重程度:注意

Message: Undefined index: image_file 消息:未定义的索引:image_file

Filename: controllers/men.php 文件名:controllers / men.php

Line Number: 23 行号:23

Backtrace: 回溯:

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

File: C:\\xampp\\htdocs\\CodeIgniter-3.1.8\\index.php Line: 315 Function: require_once 文件:C:\\ xampp \\ htdocs \\ CodeIgniter-3.1.8 \\ index.php行:315功能:require_once

This is my view,controller and model code 这是我的观点,控制器和模型代码

1) View 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) Controller 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) Model 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";
        }
    } 
}

此错误可能是由于缺少表单标记的属性而引起的,请在表单标记中添加-> 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>

First of all if you want to upload file you need to add enctype attribute to the form. 首先,如果要上传文件,则需要在表单中添加enctype属性。 Add enctype= multipart/form-data attribute to the form and try again. 将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>

在表单中使用此属性enctype =“ multipart / form-data”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM