如何在Codeigniter中使用jquery ajax插入多个图像名称并将其移动到文件夹中?

时间:2019-01-02 12:48:34

标签: jquery ajax codeigniter mysqli

在我的代码中,我有一个输入字段<input type="file" id="product_image" name="product_image[]" multiple>,当我们将图像移动到文件夹中时,我想在其中输入多个图像名称到数据库中。

当我使用var_dump($_FILES)时,它会向我显示 array(1) { ["product_image"]=> array(5) { ["name"]=> array(1) { [0]=> string(9) "img3.jpeg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpd6jSmA" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(40711) } } }

查看:

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            product_name = $("#product_name").val();
            quantity = $("#quantity").val();
            var formData = new FormData();
            $.each($("#product_image"), function (i, obj) {
                $.each(obj.files, function (j, file) {                    
                    formData.append('product_image[' + i + ']', file);
                });
            });
            formData.append('product_name', product_name);
            formData.append('quantity', quantity);
            $.ajax({
                type:"POST",
                data:formData,
                processData: false,
                contentType: false,
                url:"<?php echo base_url(); ?>admin/products",
                success:function(data){
                    $("#success_upload").html(data);
                }
            });
        });
    });
</script>

<input type="text" class="form-control" id="product_name" name="product_name">
<input type="text" class="form-control" id="quantity" name="quantity">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">

控制器:

public function products()
{   
    $dataInfo = array();
    $files = $_FILES;
    $cpt = count($_FILES['product_image']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['product_image']['name']= $files['product_image']['name'][$i];
        $_FILES['product_image']['type']= $files['product_image']['type'][$i];
        $_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
        $_FILES['product_image']['error']= $files['product_image']['error'][$i];
        $_FILES['product_image']['size']= $files['product_image']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        $this->upload->do_upload('product_image');
        $dataInfo[] = $this->upload->data();
    }

    $data = array(
            'product_name' => $this->input->post('product_name'),
            'quantity' => $this->input->post('quantity'),
            'product_image' => implode(",",array_column($dataInfo, 'product_image')),
        );
    $sql = $this->db->insert('add_product',$data);
    if($sql == true)
    {
        echo '<p style="color:green;">New Product Added</p>';
    }
    else
    {
        echo '<p style="color:red;">Unable to Proceed!</p>';
    }
}

private function set_upload_options()
{   
    $config = array();
    $config['upload_path'] = ''.base_url().'resource/product/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '1024';
    $config['overwrite']     = FALSE;
    return $config;
}

现在,数据库中仅存储product_name and quantity,但我也想将product_image存储到我的数据库中,该数据库由逗号(,)分隔,例如img1.png,img2.png。请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

首先,检查图像是否正在上传。

如果上传,则将图像名称推送到数组中。

public function products()
{   
    $images_name = array();
    $files = $_FILES;
    $cpt = count($_FILES['product_image']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['product_image']['name']= $files['product_image']['name'][$i];
        $_FILES['product_image']['type']= $files['product_image']['type'][$i];
        $_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
        $_FILES['product_image']['error']= $files['product_image']['error'][$i];
        $_FILES['product_image']['size']= $files['product_image']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        if($this->upload->do_upload('product_image'))
        { 
            $dataInfo = $this->upload->data();
            array_push($images_name, $dataInfo['file_name']);
        }
    }

    $data = array(
            'product_name' => $this->input->post('product_name'),
            'quantity' => $this->input->post('quantity'),
            'product_image' => implode(",", $images_name)
        );
    $sql = $this->db->insert('add_product', $data);
    if($sql)
    {
        echo '<p style="color:green;">New Product Added</p>';
    }
    else
    {
        echo '<p style="color:red;">Unable to Proceed!</p>';
    }
}