-1

I am willing to upload multiple images on CodeIgniter, and I only know single image upload. Here's my code on my model :

private function _uploadImage()
{
    $config['upload_path']          = './upload/product/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['file_name']            = $this->product_id;
    $config['overwrite']            = true;
    $config['max_size']             = 1024; // 1MB

    $this->load->library('upload', $config);

    if ($this->upload->do_upload('image')) {
        return $this->upload->data("file_name");
    }
    
    return "default.jpg";
}

Can you tell me how to do it?

  • Does this answer your question? [Multiple image upload with CodeIgniter](https://stackoverflow.com/questions/40778683/multiple-image-upload-with-codeigniter) – Don't Panic Jun 22 '21 at 08:41

2 Answers2

0

try this : View:

<input type='file' name='files[]' multiple=""> <br/><br/>
<input type='submit' value='Upload' name='upload' />

Controller :

public function uploadImage() { 
   
      $data = [];
   
      $count = count($_FILES['files']['name']);
    
      for($i=0;$i<$count;$i++){
    
        if(!empty($_FILES['files']['name'][$i])){
    
          $_FILES['file']['name'] = $_FILES['files']['name'][$i];
          $_FILES['file']['type'] = $_FILES['files']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['files']['error'][$i];
          $_FILES['file']['size'] = $_FILES['files']['size'][$i];
  
          $config['upload_path'] = 'uploads/'; 
          $config['allowed_types'] = 'jpg|jpeg|png|gif';
          $config['max_size'] = '5000';
          $config['file_name'] = $_FILES['files']['name'][$i];
   
          $this->load->library('upload',$config); 
    
          if($this->upload->do_upload('file')){
            $uploadData = $this->upload->data();
            $filename = $uploadData['file_name'];
   
            $data['totalFiles'][] = $filename;
          }
        }
   
      }
   
      $this->load->view('imageUploadForm', $data); 
   }
vins
  • 449
  • 4
  • 13
  • yes, here's the error: count(): Parameter must be an array or an object that implements Countable also this Trying to access array offset on value of type int – Terizla Smith Jun 21 '21 at 12:14
0

Try this:

On the view -

<input type="file" class="custom-file-input" id="file" name="file[]" multiple>
   
<label class="custom-file-label" for="customFile">Choose file</label>
                        </div>

On the Controller:

if ($multiplefile = $this->request->getFiles()) {
             foreach($multiplefile['file'] as $file)
             {   
                if ($file->isValid() && ! $file->hasMoved()) 
      {
          $newName = $file->getRandomName();
          $file->move(WRITEPATH.'uploads', $newName);
              $data = [
                'report_id' =>  $report_id,
                'document_name' => $newName,
                'document_type'  => $file->getClientMimeType()
              ];
              $save = $builder->insert($data);
              $msg = 'Files has been uploaded';
             }

             echo 'no';
        }
        echo 'no';

Incase it brings an error apply a regex on the filename to remove the whitespaces.