0

I have tried almost all the forums on google but can't find a solution to this problem, i have a product upload form which have two upload images options as well, but i can't to seem upload any image to database, neither i can't get the upload path method working. help me out please. what is the best possible way to upload images to db?

**Controller File**
    <?php 

        class Admin extends CI_Controller{

            function __construct(){
                parent::__construct();
                $this->load->model('admin_model');
            }


            public function index(){
                $data['cats'] = $this->admin_model->get_cats();
                $data['brands'] = $this->admin_model->get_brands();
                $this->load->view('admin_view', $data, $data);

                $data['upload_data'] = $this->upload->data();
                $image = base_url("uploads/". $data['raw_name'] . $data['file_ext']);
                $_POST['product_img1'] = $image;
                //$image_url = $this->upload->data('full_path');

                $product = array(
                'product_name'  => $this->input->post('name'),
                'brand_id'      => $this->input->post('brands'),
                'cat_id'        => $this->input->post('catagories'),
                'product_price' => $this->input->post('price'),
                'product_desc'  => $this->input->post('desc'),
                'product_img1'  => $this->input->post('img')
                );
                //$insert_id = $this->admin_model->form_insert($product);


                /**
                $config = array(
                'upload_path' => "./images/",
                'allowed_types' => "gif|jpg|png|jpeg",
                'overwrite' => true
                ); 

                $this->load->library('upload', $config);

                $data = $this->upload->data();
                $image = base_url("./uploads/". $data['raw_name'] . $data['file_ext']);
                $_POST['image'] = $image; 
                $this->load->model('admin_model');
                **/
                //if (!empty($_POST)) {
                    // Loading model
                    //$this->upload->do_upload();
                    //$data = array('product_img1' => $this->upload->data());
                    //$file_data = $this->upload->data();
                    //$data['product_img1'] = base_url().'/uploads/'.$file_data['file_name'];



                        //$product_img1 = $_FILES['product_img1']['name'];
                        //$product_img2 = $_FILES['product_img2']['name'];

                        //$temp_name1 = $_FILES['product_img1']['tmp_name'];
                        //$temp_name2 = $_FILES['product_img2']['tmp_name'];

                        //m_checkstatus(conn,       identifier)ove_uploaded_file($temp_name1, "uploads/$product_img1");
                        //move_uploaded_file($temp_name2, "uploads/$product_img2");

                    //$this->admin_model->insert($data);    


                    // Calling model
                    //$id = $this->admin_model->form_insert($data);  
                    //}
     }
    }
    ?>
    **Model File**
    <?php
    class admin_model extends CI_Model{
    function __construct() {
    parent::__construct();
    }
            function form_insert($product){
            // Inserting in Table(students) of Database(college)
            $insert = $this->db->insert('products', $product);
            return $insert;
            }

        function get_cats(){
            $this->db->select("*");
            $this->db->from("catagories");
            $query = $this->db->get();
            return $query->result_array();
            }

        function get_brands(){
            $this->db->select("*");
            $this->db->from("brands");
            $query = $this->db->get();
            return $query->result_array();
            }

        }
    ?>
George Kagan
  • 5,913
  • 8
  • 46
  • 50
  • Possible duplicate of [Image upload to MySQL database blob in codeigniter](http://stackoverflow.com/questions/28621271/image-upload-to-mysql-database-blob-in-codeigniter) – Lucas Meine Nov 28 '16 at 21:04

2 Answers2

0

This question was already answered here

$this->input->post('img') in model won't work to retrieve the image information. Because the images are stored in $_FILES not in $_POST. So you need to use the upload library in codeignitor like below.

Also, make sure that your form contains the enctype="multipart/form-data" attr and your column type is blob in the database.

Community
  • 1
  • 1
Lucas Meine
  • 1,524
  • 4
  • 23
  • 32
0

i will give you an example.This is the controller section.

public function add()
{       

        if (empty($_FILES['image']['name']))
             {$this->session->set_flashdata('yes', 'Please Upload an image');
                redirect($_SERVER['HTTP_REFERER']);
             }
        else
            {
                $target_dir = "images/products/";
                $target_file = $target_dir . time().basename($_FILES["image"]["name"]);
                $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                $imgName = time().basename($_FILES["image"]["name"]);
    move_uploaded_file($_FILES["image"]["tmp_name"],$target_file);
$this->adminmodel->addproducts($imgName);
}
}

Model section

public function addproducts($imgName)
    {   
        $data = array(
                'name'              => $this->input->post('name'),
                'category'      => $this->input->post('category'),

                'image'             => $imgName

                    );
        $this->db->insert('products', $data);
        $this->session->set_flashdata('yes', 'Product added successfully');
        redirect('admin/products/productlist');
    }

View Section(Form)

form role="form" action="admin/products/add" method="post" enctype="multipart/form-data" id="contact-form">

Your Form should contain enctype="multipart/form-data"

Yadhu Babu
  • 1,503
  • 2
  • 13
  • 25