如何在Codeigniter ..中上载多个文件的多个输入?

时间:2019-01-28 13:01:46

标签: codeigniter-3

我不明白我的代码有什么问题。请,任何人都可以帮助我解决这个问题。这里的“我的文件”不是“上传到数据库并插入数据库”。

查看页面代码:-

   <div class="col-md-12">
        <div class="col-md-4">
            <button type="button" id="add_design_image" class="btn btn-md btn-primary"><i class="fa fa-plus-circle" aria-hidden="true"></i> Add Image</button>

        </div>
        <div class="col-md-8" id="adddesignfile">
             Upload Design 

            <input type="file" name="image[]" id="image"/>
        </div>
    </div>

用于添加多个文件的脚本代码:-

<script type="text/javascript">$(function(){
 $('#add_design_image').click(function () {
 $('#adddesignfile').append(' Upload Design <br>  <input type="file" name="image[]" id="image" />');
    });

});

控制器代码:-

 if(@$_FILES["image"]["name"]!=""){
       if (basename($_FILES["image"]["name"])) {
         $design_images_arr = array();
           foreach ($_FILES['image']['name'] as $key => $val) {   
            /* $picName = basename($_FILES["image"]["name"]); */
            $string = str_replace(' ', '-', basename($_FILES["image"]["name"][$key]));
            $picName = preg_replace('/[^a-zA-Z0-9_.]/', '', $string);
            $folder = time() . "_" . rand(0, 99) . "_" . $picName;
            $path = "design/" . $folder;
            $config['allowed_types'] = 'jpg|JPG|jpeg|JPEG|png|PNG|Gif|GIF|PSD|psd|ai|AI';
            $config['source_image'] = $path;
            $this->load->library('image_lib', $config);
            $sourcePic = $_FILES["image"]["tmp_name"][$key];
            if(move_uploaded_file($sourcePic, $path)){
                 $design_images_arr[] = $folder;
            }
            if ($this->input->post('some_status')) {
                $new_design_generated_id = 'CPID' . date('d') . '-' . rand(0, 99999);
            } else {
                $new_design_generated_id = 'NULL';
            }
        }
         $image_names = implode(',', $design_images_arr);
             if ($image_names != '') {
                        $designimage = explode(',', $image_names);
                        foreach ($designimage as $image) {
                            $this->user_model->insert_multiple_design_image([
                                                            'design_msg_id' => $res, 
                                                            'design_order_id' => $this->input->post('order_id'),
                                                            'design_user_id' =>$this->session->userdata('uid'),
                                                            'design_image'=>$image
                                                            ]);
                 }
            }

    }

 }

0 个答案:

没有答案