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_fi开发者_运维知识库le', 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?
File uploads go to $_FILES instead of $_POST
精彩评论