如何使用codeigniter将两个图像上传到两个字段

时间:2017-06-06 21:37:37

标签: codeigniter file upload

我想使用Codeigniter将两张或多张图片在单独的字段中上传到数据库中。

但这里只有一个正在上传..任何人都可以帮助我..

我的控制器


class Products extends CI_Controller {

       public function __construct()
        {
                // Call the CI_Model constructor
                parent::__construct();

                $this->load->model('Product_model');


        }

    public function save()
            {
                    $config['upload_path']          = './uploads/';
                    $config['allowed_types']        = 'jpg|png';
                    $config['max_size']             = 5024;
                    $config['encrypt_name']         =TRUE;  


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

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

                            echo var_dump($error ); die;
                    }
                    else
                    {

                        $file_data = array('upload_data' => $this->upload->data());

                    if($this->Product_model->addProducts($file_data))
                    {


                        $this->load->view('success_view');
                    }
                    else
                    {


                        $this->load->view('failure_view');
                    }
                }

这是我的模特


    public function addProducts($file_data)
            {
                    $data=array(
                            'pr_name'=>$_POST['pr_name'],

                            'pr_code'=>$_POST['pr_code'],



                            'photo_file'=>$file_data['upload_data']['file_name'],
                            'photo_file2'=>$file_data['upload_data']['file_name'],


                            );

                    return $this->db->insert('products', $data);
            }

这是我的观点

    <div class="container">

<div class="row">
    <div class="col-md-6">
        <form class="form-horizontal" method="post" enctype="multipart/form-data" action="<?php echo site_url('Products/save');?>">
          <div class="form-group">
            <label for="exampleInputEmail1">Product Name</label>
            <input type="text" name="pr_name" class="form-control" id="exampleInputEmail1" placeholder="Product Name">
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Product Model</label>
            <input type="text" name="pr_code" class="form-control" id="exampleInputPassword1" placeholder="Product Model">
          </div>

          <div class="form-group">
            <label for="exampleInputFile">Product Image 1</label>
            <input type="file" name="userfile" id="exampleInputFile" >

          </div>

          <div class="form-group">
            <label for="exampleInputFile">Product Image 2</label>
            <input type="file" name="userfile2" id="exampleInputFile" >

          </div>




          <button type="submit" class="btn btn-default">Submit</button>
        </form>
    </div>
</div>

我的数据库

CREATE TABLE `products` (
  `pr_id` int(5) NOT NULL,
  `pr_name` varchar(200) NOT NULL,
  `pr_code` varchar(200) NOT NULL,
  `photo_file` varchar(255) NOT NULL,
  `photo_file2` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

请帮我上传到单独的数据库字段中作为两个单独的文件

1 个答案:

答案 0 :(得分:2)

使用此功能并享受;)

public function save() {
  $data = array();

  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'jpg|png';
  $config['max_size'] = 5024;
  $config['encrypt_name'] = true;  

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

  $data['pr_name'] = $this->input->post('pr_name');
  $data['pr_code'] = $this->input->post('pr_code');

  if (!$this->upload->do_upload('userfile')) {
    $error = array('error' => $this->upload->display_errors());
  } else {
    $fileData = $this->upload->data();
    $data['userfile'] = $fileData['file_name'];
  }

  if (!$this->upload->do_upload('userfile2')) {
    $error = array('error' => $this->upload->display_errors()); 
  } else {
    $fileData = $this->upload->data();
    $data['userfile2'] = $fileData['file_name'];
  }
  // Verify the data using print_r($data); die;
  $result = $this->Product_model->addProducts($data);
  if ($result) {
    $this->load->view('success_view');
  } else {
    $this->load->view('failure_view');
  }
}