首页 > 解决方案 > 使用 codeigniter 和 filepond 在数据库中插入多个图像

问题描述

以下是我的控制器在此我使用两个 if 语句,一个用于多个图像,另一个用于特色图像。我的图像很好地上传到一个文件夹中,多个名称没有插入数据库中...只有一个文件名已插入数据库...

 public function uploadApi()
{

    if (isset($_FILES['userfile'])) {
        $config['upload_path']          = 'uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 200000;
        $config['max_width']            = 2024;
        $config['max_height']           = 1768;

        $this->upload->initialize($config);        
        $this->load->library('upload', $config);
        $this->upload->do_upload('userfile');
        $data = array( $this->upload->data()); 
        $this->m->update_post($data[0]['file_name']);   

    }
    if(isset($_FILES['userfile1'])) {
       $config['upload_path']          = 'uploads/';
       $config['allowed_types']        = 'gif|jpg|png';
       $config['max_size']             = 200000;
       $config['max_width']            = 2024;
       $config['max_height']           = 1768;

       $this->upload->initialize($config);        
       $this->load->library('upload', $config);
       $this->upload->do_upload('userfile1');
       $data = array( $this->upload->data());     
       $this->m->update_feature($data[0]['file_name']);   

   }
}

这是一个模型..

            #-Update images Post-#
 public function update_post($picture) {
$post = array(
    'post_images'=>$picture,
);  
$this->db
->where('post_status','draft')
->update('post',$post); 
return true; 
     }

public function update_feature($picture) {
$post = array(
    'post_featured_image'=>$picture,
);  
$this->db
        // ->set('post_created', 'NOW()', FALSE)

->where('post_status','draft')
->update('post',$post);
return true; 
}

文件池插件脚本

     FilePond.registerPlugin(
        FilePondPluginFileValidateSize,
        FilePondPluginImageExifOrientation,
        FilePondPluginImageCrop,
        FilePondPluginImageResize,
        FilePondPluginImagePreview,
        FilePondPluginImageTransform
        );    
    // Set default FilePond options
    FilePond.setOptions({
        // maximum allowed file size
        maxFileSize: '50MB',
        imagePreviewHeight: 100,
        imagePreviewWidth: 200,
        instantUpload: true,
        // crop the image to a 1:1 ratio
        imageCropAspectRatio: '1:1',
        // upload to this server end point
        server: {
            url: '<?php echo base_url() ?>Admin/uploadApi',
        }        
    });
   
    var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
    var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
   
**This is a view ..**

  <form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
  
  <div class="col-md-6">
                          <div class="form-group">
                            <label>Upload images</label>
                            <input type="file" 
                            class="filepond"
                            name="userfile"
                            multiple
                            data-max-file-size="5MB"
                            data-max-files="50" data-validation="required extension" />   

                        </div>

                        <div class="form-group">
                            <label>Feature image</label>
                            <input type="file" 
                            class="filepond"
                            name="userfile1"                            
                            data-max-file-size="5MB"
                            data-validation="required extension"
                            />
                        </div>
                        
                        </form>

标签: phpjqueryhtmlcodeigniter

解决方案


请在控制器中尝试此操作

 function uploadApi() {
    $image = $_FILES;
    foreach ($image as $key => $img) {
        if (!is_dir('./Uploads/')) {
            mkdir('./Uploads/', 0777, TRUE);
        }
        if (!empty($img['name'])) {
            $config['upload_path'] = './Uploads/Products/';
            $config['allowed_types'] = '*';
            $config['max_size'] = '100';
            $config['max_width'] = '1024';
            $config['max_height'] = '768';
            $config['overwrite'] = TRUE;
            $config['file_name'] = date('U') . '_' . $img['name'];
            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if (!$this->upload->do_upload($key)) {
                $error = array('error' => $this->upload->display_errors());
                print_r($error);
                die;
            } else {
                if ($this->upload->do_upload($key)) {
                    $image_data = $this->upload->data();
                    $update["userfile"] = $config['file_name'];
                    $res = $this->m->update_post($update);
                }
            }
        }
    }
    $this->load->view('imgtest');
}

推荐阅读