在数据库中插入图像(codeigniter)

时间:2017-06-17 02:51:40

标签: php mysql codeigniter

在想要将表单数据插入数据库的那一刻,它完成了一切,但问题是它插入了所有字段而不是图像

表单视图:

<form name="form_cot" action="<?php echo base_url("article/insert")?>" method="POST">

  <div class="form-group">
  <label for ="tipotrabajo">Tipo de Trabajo: </label>
 <input type="radio" name="tipotrab" value="0" required> Fabricación
 <input type="radio" name="tipotrab" value="1"> Reparación
  </div>

  <div class="form-group">
     <label for ="origenmat">Origen del Material: </label>
 <input type="radio" name="origenmat" value="0" required> Empresa
 <input type="radio" name="origenmat" value="1"> Cliente
  </div>

  <div class="form-group">
    <label for="material">Material</label>
    <input type="text" name="material" value="<?= @set_value('material') ?>" class="form-control" placeholder="Ejemplo: acero,cobre" required>
  </div>
  <div class="form-group">
    <label for="alto">Alto</label>
    <input type="number" name="alto" class="form-control" required>
  </div>
  <div class="form-group">
    <label for="ancho">Ancho</label>
    <input type="number" name="ancho" class="form-control" required>
  </div>
  <div class="form-group">
    <label for="largo">Largo</label>
    <input type="number" name="largo" class="form-control" required>
  </div>
  <div class="form-group">
    <label for="cantidad">Cantidad</label>
    <input type="number" name="cantidad" class="form-control" required>
  </div>
  <div class="form-group">
  <label for="imagen">Imagen Referencia</label>
    <input type="file" name="userfile" required=''>
  </div>



  <center><input type="submit" value="Enviar" name="enviar" class="btn btn-primary"></center>
</form>

控制器:

public function insert(){

  $post = $this->input->post();
  $this->load->model('file');
  $file_name = $this->file->UploadImage();
  $post['file_name'] = $file_name;

  $bool = $this->cotizacion->insert($post);
  if($bool){
     header("Location: " . base_url() . "profile"); 
  }else{
    header("Location: " . base_url() . "article/nuevo"); //no se guardo
  }

  }

模型file.php

            public function UploadImage(){

            $config['upload_path']          = './public/img/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

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

            if ( ! $this->upload->do_upload('userfile'))
            {
                    $error = array('error' => $this->upload->display_errors());

                    $this->load->view("/clientes/nuevo", $error);
            }
            else
            {
                    $file_data = $this->upload->data();

                     $file_name = $file_data['file_name'];
                     return $file_name;
            }
    }

请帮助哪里出错?

1 个答案:

答案 0 :(得分:0)

首先添加表单标记

发送编码为“multipart / form-data”的表单数据:

文件上传文档click here

> sdkmanager --licenses