1

I want to upload files after the successful insertion of some data into the database but the file uploading does not work.

The following is my code for file upload but it's not uploading.

Controller

public function saveReceipt(){
    $doc=$this->receipt_m->saveReceipt_m();
    if($doc){
      $countfiles = count($_FILES['attatchments']['name']);

      for($i=0;$i<$countfiles;$i++){

        if(!empty($_FILES['attatchments']['name'][$i])){

          // Define new $_FILES array - $_FILES['file']
          $_FILES['file']['name'] = $_FILES['attatchments']['name'][$i];
          $_FILES['file']['type'] = $_FILES['attatchments']['type'][$i];
          $_FILES['file']['tmp_name'] = $_FILES['attatchments']['tmp_name'][$i];
          $_FILES['file']['error'] = $_FILES['attatchments']['error'][$i];
          $_FILES['file']['size'] = $_FILES['attatchments']['size'][$i];

          // Set preference
          $config['upload_path'] = base_url("assets/attachments"); 
          $config['allowed_types'] = 'pdf|txt';
          $config['max_size'] = '5000'; // max_size in kb
          $config['file_name'] = $_FILES['attatchments']['name'][$i];

          //Load upload library
          $this->load->library('upload',$config); 
          $arr = array('msg' => 'something went wrong', 'success' => false);
          // File upload
          if($this->upload->do_upload('file')){

           $data = $this->upload->data(); 
           $arr = array('msg' => 'Image has been uploaded successfully', 'success' => true);

          }
        }

      }
      echo json_encode($arr);
    }else{
      $response=array("status"=>false);
    }
   }

View

<form class="horizontal-form" name="frmSaveReceipt" id="frmSaveReceipt"  enctype="multipart/form-data"> 
     <input type="file" name="attatchments[]" id="file" multiple="multiple">
   </form 
1
  • so you get returned "status" false ? that means $doc doesn't return anything, which means you need to analyze in your model receipt_m the function saveReceipt_m(), please add the relevant code... Commented Nov 17, 2019 at 12:45

2 Answers 2

1

Use move_uploaded_file() to upload multiple file....

$filename = array();
for($i=0;$i<count($_FILES['attatchments']['name']);$i++){

if(!empty($_FILES['attatchments']['name'][$i])){
 move_uploaded_file($_FILES['attatchments']['tmp_name'][$i],
'assets/attachments'.$_FILES['attatchments']['name'][$i]);
 $filename[$i] = $_FILES['attatchments']['name'][$i];
 }
}

Now use loop on $filename to upload multiple file in your database..

Sign up to request clarification or add additional context in comments.

Comments

0

below code working for me please try this Controller code is below

$info=$this->input->post();
$data = array();
  // If file upload form submitted

$countfiles = count($_FILES['galleryImage']['name']);

 for($i=0;$i<$countfiles;$i++){

if(!empty($_FILES['galleryImage']['name'][$i])){

      // Define new $_FILES array - $_FILES['file']
  $_FILES['file']['name'] = $_FILES['galleryImage']['name'][$i];
  $_FILES['file']['type'] = $_FILES['galleryImage']['type'][$i];
  $_FILES['file']['tmp_name'] = $_FILES['galleryImage']['tmp_name'][$i];
  $_FILES['file']['error'] = $_FILES['galleryImage']['error'][$i];
  $_FILES['file']['size'] = $_FILES['galleryImage']['size'][$i];

      // Set preference
  $config['upload_path'] = './images/'; 
  $config['allowed_types'] = 'jpg|jpeg|png|gif';
      $config['max_size'] = '5000'; // max_size in kb
      $config['file_name'] = $_FILES['galleryImage']['name'][$i];

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

      // File upload
      if($this->upload->do_upload('file')){
        // Get data about the file
        $uploadData = $this->upload->data();
        $filename = $uploadData['file_name'];

        // Initialize array
        //$data['filenames'][] = $filename;

        $color=array(
         "pageId"=>$info["pageId"],
         "meta_key"=>"gImage",
         "meta_value"=>$filename,
         "status"=>1
       );
        $insert=$this->pm->insert_data($table,$color);
      }
    }

  }

and views code is

 <?php
$url=site_url('pages/SavePageAttribute');
$options = array('class' => 'form-inline', 'id' => 'category', 'method' => 'post','style'=>'margin:10px');
echo form_open_multipart($url, $options);
?>
  <tr>
     <td> 
      <?php
         echo form_label('Gallery Image', 'galleryImage'); ?></td>
        <td> 
        <input class="form-control" type="file" name="galleryImage[]" id="galleryImage" multiple="true">
         </td>
       </tr>
<?php echo form_close(); ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.