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

时间:2015-12-23 08:05:44

标签: php mysql codeigniter

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

我的代码是:

控制器 - 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());
            }
        }
    }

模型 - productModel.php

public function insert_products_to_db($data)
{
    return $this->db->insert('product_info', $data);
}

查看 - insertProduct.php

<?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>

我的数据库表包含5个字段p_id,名称,制造商,价格和图像。运行此代码后,当我点击添加它时,不显示任何错误或不重定向到控制器,但它保持在同一页面上,uploadProductImages中没有任何图像,也没有新产品将插入数据库表

对此新学员的任何帮助将不胜感激。

0 个答案:

没有答案