2016-11-28 93 views
0

我已经尝试了几乎所有的论坛上谷歌,但无法找到解决方案 这个问题,我有一个产品上传表格,其中也有两个上传 图像选项,但我不能上传任何图像到 数据库,我也无法获得上传路径的方法工作。请帮我 出来。什么是最好的方式上传图像到数据库?无法将图像上传到数据库codeigniter

**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(); 
      } 

     } 
    ?> 
+0

的[图像上传到在笨MySQL数据库团块]可能的复制(http://stackoverflow.com/questions/28621271/image-upload-to-mysql-database-blob-in-codeigniter) –

回答

0

这个问题已经回答here

$这个 - >输入 - >后( 'IMG')的模式将无法正常工作,以获取图像信息。因为图像存储在$ _FILES而不是$ _POST中。因此,您需要像下面一样使用codeignitor中的上传库。

此外,请确保您的形式包含ENCTYPE = “的multipart/form-data的” ATTR和字段类型是BLOB在数据库中。

0

我会给你一个例子。这是控制器部分。

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

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'); 
    } 

视图部分(表格)

形式的作用= “形式” 行动= “管理/产品/添加” 方法= “邮报” ENCTYPE =” multipart/form-data“id =”contact-form“>

您的表格应包含enctype =”multipart/form-data“