0

I'm trying to create a form that contains these multidimensional arrays:

<input type="text" name="cost[1][desc]">
<input type="text" name="cost[1][price]">
<input type="file" name="cost[1][file]">
<input type="text" name="cost[2][desc]">
<input type="text" name="cost[2][price]">
<input type="file" name="cost[2][file]">

Each 'cost' array has these three inputs: description, price and a file upload. There may be multiple 'cost' arrays (which is why I made the second parameter a number). In my CodeIgniter model I have this:

foreach($_POST as $post => $array){                
  if($post=='cost') {             
    foreach($array as $number){ 
      foreach($number as $label => $value){
        if($label=='file'){
          $config['upload_path'] = './uploads/receipts';
          $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf';
          $config['max_size'] = '4096';
          if(!empty($value['name'])){
            $this->upload->initialize($config);
            if($this->upload->do_upload($label)){
              $file = $this->upload->data();
              $path = $file['file_name'];
            }
          }
        }
        $cost_array = array('desc'=>$number['desc'],'price'=>$number['price'],'file'=>$path);
        $price = number_format($number['price'],2);
      }
      $main_array[] = $cost_array;
      $main_price[] = $price; 
    }
  }  
}
$data['cost_info'] = serialize($cost_array);
$data['extra_cost'] = array_sum($price);
$this->db->insert('reports',$data);

The values for 'desc' and 'price' enter the array (and get serialized) no problem at all - but for some reason I can't receive any file information. I removed a lot of the if statements to see if that was the issue, but it's not. If I changed the file input HTML tag so that its name was 'cost_1_file', and if I changed the PHP model to:

if($this->input->post('cost_1_file')==''){
  echo 'Nope';
}

it echoes out the statement - meaning that it's not receiving any file upload data at all. I've made sure my form is a form_open_multipart. Does anybody know where I'm going wrong?

1
  • check page roytuts.com/codeigniter-multiple-files-upload/ Commented Jun 8, 2016 at 11:16

1 Answer 1

2

File uploads go to $_FILES instead of $_POST

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

1 Comment

I realised this 5mins after posting, haha. Thanks!

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.