图片到数据库上传不在Codeigniter中工作

时间:2017-07-04 12:20:44

标签: php codeigniter image-uploading codeigniter-3

我正在使用CodeIgniter框架建立一个网站,以便用户可以将网站上的产品上传到数据库。我正在尝试创建一个函数,以便用户可以将图片上传到我的数据库,但它并没有真正起作用。

这里有一些信息: 数据库表名:产品 我想要将图片存储在的DB表列名称:product_foto 图片文件夹:上传

$(window).on('resize', function(){ // recalculate when resizing window
  if (window.innerWidth > 768) { // only in 'desktop' version(s)
    $('.navbar-nav').find('> li:not(.hidden-sm)').each(function(){
      var width = $(this)[0].getBoundingClientRect().width; // for each valid 'li', take its 'a' descendant width
      if (width%1 !== 0) {$(this).width(Math.ceil(width));} // if width isn't already an integer, round up
      console.log(width, 'resize: '+(width%1 !== 0));
    });
  }
}).resize();

我的模特档案:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<nav class="navbar navbar-default">
  <div class="container-fluid">
    <!-- Collect the nav links, forms, and other content for toggling -->
    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Link One<span class="sr-only">(current)</span></a></li>
        <li><a href="#">Link Two, Long</a></li>
        <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
          <ul class="dropdown-menu">
            <li><a href="#">Action</a></li>
            <li><a href="#">Another action</a></li>
            <li><a href="#">Something else here</a></li>
          </ul>
        </li>
        <li class="hidden-sm"><a href="#">Link Three: too long to fit, hide on small devices</a></li>
      </ul>
    </div><!-- /.navbar-collapse -->
  </div><!-- /.container-fluid -->
</nav>

我的观点文件:

My controller: 
    <?php
     defined('BASEPATH') OR exit('No direct script access allowed');
     class Product extends CI_Controller { 

     var $data = array();

     public function __construct()
     {
      parent::__construct();
      $this->load->model('product_model');
      $this->load->helper(array('form', 'url'));
     }

     public function product_form()
     {
      $save = array(
          'product_naam'          => $this->input->post('product_naam'),
          'product_beschrijving'          => $this->input->post('product_beschrijving'),
          'product_categorie'   => $this->input->post('product_categorie'),
          'ophaal_plaats'   => $this->input->post('ophaal_plaats'), 
          'product_foto'  => $this->input->post('product_foto'),
          'date_created' => date('Y-m-d'),
          'date_updated' => date('Y-m-d')
           );

      $this->product_model->saveProduct($save);
      redirect('https://kadokado-ferran10.c9users.io/AlleCadeausController');
     }


     public function upload(){
      $config['upload_path'] = './upload/';
      $config['allowed_types'] = 'jpg|jpeg|png';

      $this->load->library('upload', $config);
      if(!$this->upload->do_upload('file')){
        $this->db->insert('products', array(
           'product_foto' => $this->upload->file_name
           ));
       $error = array('error'=>$this->upload->display_errors());
       $this->load->view('product_form', $error);
      }else{
       $file_data = $this->upload->data();
       $data['img'] = base_url().'/upload/'.$file_data['file_name'];
       header('location:https://kadokado-ferran10.c9users.io/Product/');
       }
     }

    }

当我提交表单时,图片只会添加到上传文件夹中,但不会被添加到数据库列中。

1 个答案:

答案 0 :(得分:1)

更改您的upload()功能,如下所示

public function upload(){
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = 'jpg|jpeg|png';

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

    if(!$this->upload->do_upload('userfile')){
        $error = array('error'=>$this->upload->display_errors());
        $this->load->view('product_form', $error);
    }else{
        $file_data = $this->upload->data();
        $this->db->insert('products', array(
            'product_foto' => $file_data['file_name']
            ));
        $data['img'] = base_url().'/upload/'.$file_data['file_name'];
        header('location:https://kadokado-ferran10.c9users.io/Product/');
    }
}