0

Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...

 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']);   

   }
}

This is a model ..

            #-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 plugin script

     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>
Sayed Mohd Ali
  • 2,156
  • 3
  • 12
  • 28
  • What are you getting in - echo $data[0]['file_name']; -in both the function? – BILAL MALIK Feb 20 '19 at 05:47
  • nothing display – varun pawar Feb 20 '19 at 05:55
  • Debug it ... do you get anything in $data?.... print_r($data); – BILAL MALIK Feb 20 '19 at 05:59
  • yeah it display the full name of the image(jquery-logo_-_Copy1.png) – varun pawar Feb 20 '19 at 06:08
  • Now in model, just before the query, try to print $picture to see if you get the name there – BILAL MALIK Feb 20 '19 at 06:11
  • Myabe you're not fetching it correctly in $this->m->update_feature($data[0]['file_name']);, Can you post print_r($data); output here? – BILAL MALIK Feb 20 '19 at 06:13
  • you might not be getting $data[0] probably because you dont have an array, so add [] in the name field of the view as ... name="userfile1[]" and name="userfile[]", change it wherever you have used it, including pond script. – BILAL MALIK Feb 20 '19 at 06:17
  • Array ( [0] => Array ( [file_name] => c3.jpg [file_type] => image/jpeg [file_path] => C:/xampp/htdocs/blog/uploads/ [full_path] => C:/xampp/htdocs/blog/uploads/c3.jpg [raw_name] => c3 [orig_name] => c.jpg [client_name] => c.jpg [file_ext] => .jpg [file_size] => 103.79 [is_image] => 1 [image_width] => 566 [image_height] => 566 [image_type] => jpeg [image_size_str] => width="566" height="566" )) – varun pawar Feb 21 '19 at 06:55
  • when i upload multiple images.. my uploadApi run multiple times thats why it store the latest image name in the database table... – varun pawar Feb 21 '19 at 06:58
  • editied ? var pond = FilePond.create(document.querySelector('input[name="userfile[]"]')); var pond = FilePond.create(document.querySelector('input[name="userfile1[]"]')); – BILAL MALIK Feb 21 '19 at 07:01

2 Answers2

0

For multiple image upload you should post images array like; imagename[]. Your current approach is not good.

You must try already posted answers:

Multiple image upload with CodeIgniter

Multiple image upload with Codeigniter saving only one file path to MySQL Database

https://www.codexworld.com/codeigniter-upload-multiple-files-images/

Deepak Dholiyan
  • 1,774
  • 1
  • 20
  • 35
0

Please try to this in controller

 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');
}
  • Same problem in this code.. the last image name is updated in the database table column not the all names is updated... – varun pawar Feb 21 '19 at 06:49