Php 在codeigniter中为单个记录上载多个图像

Php 在codeigniter中为单个记录上载多个图像,php,mysql,codeigniter,Php,Mysql,Codeigniter,如上所述,我想将单个产品的多个图像上传到数据库表中(该表将只包含图像路径) 我的代码是: 控制器-productController.php function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); $this->load->model('productModel'); } public function index() {

如上所述,我想将单个产品的多个图像上传到数据库表中(该表将只包含图像路径)

我的代码是:

控制器-
productController.php

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
    $this->load->model('productModel');
}
public function index()
{
    $data['product_list'] = $this->productModel->get_all_products();
    $this->load->view('productDisplay', $data);
}
public function add_form()
{
    $this->load->view('insertProduct');
}
private function setup_upload_option()
{
    $config = array();
    $config['upload_path'] = './uploadProductImages/';
    $config['allowed_types'] = 'jpg|png|gif|jpeg';
    $config['encrypt_type'] = true;
    $config['overwrite'] = false;
    return $config;
}
public function insert_new_product()
{
    $this->load->library('upload');
    $files = $_FILES;
    $count = count($_FILES['images']['name']);
    for($i=0;$i<$count;$i++)
    {
        $_FILES['images']['name'] = $files['images']['name'][$i];
        $_FILES['images']['type'] = $files['images']['type'][$i];
        $_FILES['images']['tmp_name'] = $files['images']['tmp_name'][$i];
        $_FILES['images']['size'] = $files['images']['size'][$i];
        $_FILES['images']['error'] = $files['images']['error'][$i];

        $this->upload->initialize($this->setup_upload_option());
        if($this->upload->do_upload() == false)
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('insertProduct', $error);
        }
        else
        {
            $upload_data = $this->upload->data();
            $udata['name'] = $this->input->post('name');
            $udata['manufacturer'] = $this->input->post('manufacturer');
            $udata['price'] = $this->input->post('price');
            $udata['images'] = $upload_data['file_name'];
            $res = $this->productModel->insert_products_to_db($udata);
            if($res)
            {
                header('location:'.base_url()."index.php/productController/".$this->index());
            }
        }
    }
<?php if(isset($error)){echo $error;}?>
<form method="post" action="<?php echo base_url();?>index.php/productController/insert_new_product" enctype="multipart/form-data">
<table >
    <tr>
        <th >Product Name</th>
        <td ><input type="text" name="name" /></td>
    </tr>

    <tr>
        <th >Manufacturer</th>
        <td><input type="text" name="manufacturer" /></td>
    </tr>

    <tr>
        <th >Price</th>
        <td><input type="text" name="price"/></td>
    </tr>

    <tr>
        <th >Images</th>
        <td><input type="file" name="images[]" multiple=""/></td>
    </tr>

    <tr>
        <th >&nbsp;</th>
        <td><input type="submit" name="submit" value="Add" /></td>
    </tr>
</table>
</form>
查看-
insertProduct.php

function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'url'));
    $this->load->model('productModel');
}
public function index()
{
    $data['product_list'] = $this->productModel->get_all_products();
    $this->load->view('productDisplay', $data);
}
public function add_form()
{
    $this->load->view('insertProduct');
}
private function setup_upload_option()
{
    $config = array();
    $config['upload_path'] = './uploadProductImages/';
    $config['allowed_types'] = 'jpg|png|gif|jpeg';
    $config['encrypt_type'] = true;
    $config['overwrite'] = false;
    return $config;
}
public function insert_new_product()
{
    $this->load->library('upload');
    $files = $_FILES;
    $count = count($_FILES['images']['name']);
    for($i=0;$i<$count;$i++)
    {
        $_FILES['images']['name'] = $files['images']['name'][$i];
        $_FILES['images']['type'] = $files['images']['type'][$i];
        $_FILES['images']['tmp_name'] = $files['images']['tmp_name'][$i];
        $_FILES['images']['size'] = $files['images']['size'][$i];
        $_FILES['images']['error'] = $files['images']['error'][$i];

        $this->upload->initialize($this->setup_upload_option());
        if($this->upload->do_upload() == false)
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('insertProduct', $error);
        }
        else
        {
            $upload_data = $this->upload->data();
            $udata['name'] = $this->input->post('name');
            $udata['manufacturer'] = $this->input->post('manufacturer');
            $udata['price'] = $this->input->post('price');
            $udata['images'] = $upload_data['file_name'];
            $res = $this->productModel->insert_products_to_db($udata);
            if($res)
            {
                header('location:'.base_url()."index.php/productController/".$this->index());
            }
        }
    }
<?php if(isset($error)){echo $error;}?>
<form method="post" action="<?php echo base_url();?>index.php/productController/insert_new_product" enctype="multipart/form-data">
<table >
    <tr>
        <th >Product Name</th>
        <td ><input type="text" name="name" /></td>
    </tr>

    <tr>
        <th >Manufacturer</th>
        <td><input type="text" name="manufacturer" /></td>
    </tr>

    <tr>
        <th >Price</th>
        <td><input type="text" name="price"/></td>
    </tr>

    <tr>
        <th >Images</th>
        <td><input type="file" name="images[]" multiple=""/></td>
    </tr>

    <tr>
        <th >&nbsp;</th>
        <td><input type="submit" name="submit" value="Add" /></td>
    </tr>
</table>
</form>


更改此
$count=count($_文件['images']['name'])
$count=count($files['images']['name'])因为您分配了
$files=$\u个文件是,已更改,但仍不执行任何操作。它保持不变
insertProduct.php
Post您的
insertProduct.php
file@Saty请看我最新编辑的问题,我认为问题出在代码的else循环部分,即“$upload\u data=$this->upload->data()`如何保存多个图像???更改此
$count=count($_FILES['images']['name'])
$count=count($files['images']['name'])因为您分配了
$files=$\u个文件是,已更改,但仍不执行任何操作。它保持不变
insertProduct.php
Post您的
insertProduct.php
file@Saty请看我最新编辑的问题,我认为问题出在代码的else循环部分,即“$upload\u data=$this->upload->data()`如何保存多个图像???