Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with CodeIgniter file uploads and multi-dimensional arrays
    text
    copied!<p>I'm trying to create a form that contains these multidimensional arrays:</p> <pre><code>&lt;input type="text" name="cost[1][desc]"&gt; &lt;input type="text" name="cost[1][price]"&gt; &lt;input type="file" name="cost[1][file]"&gt; &lt;input type="text" name="cost[2][desc]"&gt; &lt;input type="text" name="cost[2][price]"&gt; &lt;input type="file" name="cost[2][file]"&gt; </code></pre> <p>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:</p> <pre><code>foreach($_POST as $post =&gt; $array){ if($post=='cost') { foreach($array as $number){ foreach($number as $label =&gt; $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-&gt;upload-&gt;initialize($config); if($this-&gt;upload-&gt;do_upload($label)){ $file = $this-&gt;upload-&gt;data(); $path = $file['file_name']; } } } $cost_array = array('desc'=&gt;$number['desc'],'price'=&gt;$number['price'],'file'=&gt;$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-&gt;db-&gt;insert('reports',$data); </code></pre> <p>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:</p> <pre><code>if($this-&gt;input-&gt;post('cost_1_file')==''){ echo 'Nope'; } </code></pre> <p>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 <code>form_open_multipart</code>. Does anybody know where I'm going wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload