无法在Codeigniter中使用jquery ajax上传多张图片

时间:2019-01-05 04:30:18

标签: php jquery ajax codeigniter mysqli

在我的代码中,我有一个名称为product_image的输入字段,我想在其中输入并将多个文件插入img1.jpg,img2.jpg,img3.jpg这样的数据库中。但是,现在,当我选择4 files时,即img1.jpg,img2.jpg,img3.jpg,img4.jpgimg4.jpg存储在数据库中,并且只有此文件在我的文件夹中移动时,会发生什么情况。

视图:

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            product_name = $("#product_name").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);
            $.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="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');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;
    }
    $fileName = $images;
    $data = array(
            'product_name' => $this->input->post('product_name'),
            'product_image' => implode(",",$fileName),
        );
    $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'] = FCPATH.'resource/product/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size']      = '1024';
    $config['overwrite']     = FALSE;
    return $config;
}           

那么,如何上载多个文件并将其插入数据库?请帮助我。

谢谢

0 个答案:

没有答案