php - 使用codeigniter上传图片错误

标签 php mysql

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

Severity: Notice

Message: Undefined index: image_file

Filename: controllers/men.php

Line Number: 23

Backtrace:

File: C:\xampp\htdocs\CodeIgniter-3.1.8\application\controllers\men.php Line: 23 Function: _error_handler

File: C:\xampp\htdocs\CodeIgniter-3.1.8\index.php Line: 315 Function: require_once

这是我的 View 、 Controller 和模型代码

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

<?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";
        }
    } 
}

最佳答案

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

关于php - 使用codeigniter上传图片错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51517111/

相关文章:

mysql - 为什么这个组合键没有被索引?

php :change timezone based on user

php - 切换到学说 : how to realize such query?

php - Laravel 中的模型工厂和数据库播种机有什么区别?

php - MySQL php 连接失败

PHP MySQL ETL。我应该使用 ETL 工具、存储过程还是 php 脚本?

mysql - 临时表对每个其他表都有一个计数

mysql - REGEXP 在 mysql 中不起作用

mysql - 在 SQL 上删除触发器之前。

Mysql:优化从多个范围中选择行(使用索引?)