0

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

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

This is my view,controller and model code

1) View

<!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) Model

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

3 Answers 3

2

This error may cause due to missing attribute of form tag, add -> enctype="multipart/form-data" attribute in form tag and try like:

<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>

Sign up to request clarification or add additional context in comments.

Comments

0

First of all if you want to upload file you need to add enctype attribute to the form. Add enctype= multipart/form-data attribute to the form and try again.

<!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>

1 Comment

i placed this slash "\" in first form tag may be that is why it wasnt working further
0

use this attribute in your form enctype="multipart/form-data"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.