如何使用Codeigniter插入和移动多个图像? [英] How to insert and move multiple image using codeigniter?

查看:60
本文介绍了如何使用Codeigniter插入和移动多个图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

视图:

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            product_name = $("#product_name").val();
            color = $("#colorWell").val();
            $.ajax({
                type:"POST",
                data:{"product_name":product_name, "color":color},
                url:"<?php echo base_url(); ?>admin/products",
                success:function(data){
                    alert(data);
                }
            });
        });
    });
</script>

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

控制器:

public function products()
{
    $product_name = $this->input->post('product_name');
    $color = $this->input->post('color');

    $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();
        $dataInfo[] = $this->upload->data();
    }
    $data = array(
            'product_name' => $product_name,
            'color' => $color,
            'product_image' => implode(",",$dataInfo['product_image']),
        );
    $result_set = $this->db->insert('add_product',$data);
    if($sql == true)
    {
        echo 'New Product Added';
    }
    else
    {
        echo 'Unable to Proceed!';
    }
}

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

在这段代码中,我试图插入并且想要将图像移动到文件夹中.但是现在的问题是,当我单击提交"按钮时,它会引发一个错误,如下所示:

In this code I am trying to insert and wants to move an image into a folder. But Now, problem is that when I click on submit button it throw an error as show in below:

Message: Undefined index: product_image

查询外观:

INSERT INTO `product` (`product_name`, `color`, `product_image`) VALUES ('men hoodies','#004080', NULL)

我不知道我在哪里做错了.那么,我该如何解决这个问题?请帮我.

I don't know where am I doing wrong. So, How can I solve this issue? Please help me.

谢谢

推荐答案

您没有提交文件数据. 使用formData上载文件数据,并将要添加的其他任何输入追加到formData:

You didn't submit the files data. Use formData to upload files data, and append any other input you like to add to formData :

<script>
    $(document).ready(function(){
        $("#submit").click(function(e){
            e.preventDefault();
            product_name = $("#product_name").val();
            color = $("#colorWell").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('color', color);
            $.ajax({
                type:"POST",
                data:formData,
                processData: false,
                contentType: false,
                url:"<?php echo base_url(); ?>admin/products",
                success:function(data){
                    alert(data);
                }
            });
        });
    });
</script>

这篇关于如何使用Codeigniter插入和移动多个图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆