【问题标题】:How to upload two images into two fields using codeigniter如何使用codeigniter将两张图片上传到两个字段中
【发布时间】:2017-11-08 01:46:11
【问题描述】:

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

但是这里只有一个正在上传..有人可以帮我吗..

我的控制器

类产品扩展 CI_Controller { 公共函数 __construct() { // 调用 CI_Model 构造函数 父::__construct(); $this->load->model('Product_model'); } 公共函数保存() { $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()); 回声 var_dump($error);死; } 别的 { $file_data = array('upload_data' => $this->upload->data()); if($this->Product_model->addProducts($file_data)) { $this->load->view('success_view'); } 别的 { $this->load->view('failure_view'); } }

这是我的模型

公共函数 addProducts($file_data) { $数据=数组( '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'], ); 返回 $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;

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

【问题讨论】:

标签: codeigniter file upload


【解决方案1】:

使用此功能并享受;)

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-31
    • 2016-12-16
    • 1970-01-01
    • 2017-04-08
    • 2011-12-10
    • 2016-01-06
    • 1970-01-01
    相关资源
    最近更新 更多