如何以codeigniter格式在我的数据库中上传图片?

时间:2017-06-01 10:14:09

标签: php codeigniter codeigniter-3

嘿伙计我在codeigniter视图中使用此表单,我想知道如何上传此表单中的图片。

<form action="<?php echo site_url('Product/product_form'); ?>" method="post">      

  <h2> Cadeau aanbieden</h2>

  <table class="aanbieding-cadeau">
    <tr>          
      <td><?php echo form_input(array('id'=>'product_naam', 'name'=>'product_naam', 'placeholder' => '1. Naam van het cadeau', 'size'=>25));?></td>
    </tr>
    <tr>         
      <td><?php echo($selectField);?></td>
    </tr> 
    <tr>       
      <td><?php echo form_input(array('id'=>'ophaal_plaats', 'name'=>'ophaal_plaats', 'placeholder' => '3.Kies een stad', 'size'=>25));?></td>
    </tr>
    <tr>          
      <td>
       <?php echo form_input(array('id'=>'product_foto', 'name'=>'product_foto', 'placeholder' => '3.Upload foto', 'size'=>25));?></td>
    </tr>
    <tr>        
      <td><?php echo form_textarea(array('type'=>'textarea','id'=>'product_beschrijving', 'name'=>'product_beschrijving', 'placeholder' => '5. Vertel iets over dit cadeau..', 'size'=>25));?></td>
    </tr>
    <tr>          
      <td><button type="submit" class="btn btn-primary">Cadeau aanbieden!</button>
     </td>
    </tr>     
  </table>
</form>

所以不是这个输入字段:

 <?php echo form_input(array('id'=>'product_foto', 'name'=>'product_foto', 'placeholder' => '3.Upload foto', 'size'=>25));?>

我想选择一张图片并将其发布到我的数据库中。 有人能帮我吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

enter image description here ----&gt;使用codeigniter进行图像上传
----- Codeigniter为图像上传提供内置库 enter image description here     在控制器中写入上传代码的图像我的控制器名称User.php&amp;在这里我加载codigniter'image_lib'libadrary

答案 1 :(得分:0)

首先在视图文件中的表单中添加 enctype =“multipart / form-data” 将其添加到控制器

if($_FILES["image-file"]["size"] != 0){
        $this->load->model('Upload_model');
        $path= './uploads/images';
        $img = $this->Upload_model->image_upload($path, "", '', '', '',"image-file");
    }
if(!is_array ($img)){
   $this->db->insert('your_tbl',array("image"=>$img));
}else{
print_r($img);
}

制作模型或将其粘贴到现有一个

public function image_upload($upload_path, $max_width, $max_height, $min_width, $min_height, $filename)
{
        $config['upload_path'] = $upload_path;
        $config['file_name'] = date('Ymd_his_').rand(10,99).rand(10,99).rand(10,99);
        $config['allowed_types'] = "gif|jpg|png|jpeg|pdf|docx|JPG|JPEG|PNG";
        $config['overwrite'] = FALSE;
        $config['max_size'] = '0';
        $config['max_width']  = $max_width;
        $config['max_height']  = $max_height;
        $config['min_width']  = $min_width;
        $config['min_height']  = $min_height;
        $config['max_filename']  = '0';
        $config['remove_spaces']  = FALSE;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload($filename))
            {
               //return $this->upload->display_errors();
             return $error = array('error' => $this->upload->display_errors());
            }
        else
            {
                $data = array('upload_data' => $this->upload->data());
                $config['source_image'] = $config['upload_path'].$data['upload_data']['file_name'];
                $config['quality'] = '100%';

                $this->load->library('image_lib', $config);
                return $data['upload_data']['file_name'];
            }
unset($config);
$this->image_lib->clear();
}