首页 文章

CodeIgniter文件上传和多维数组的问题

提问于
浏览
0

我正在尝试创建一个包含这些多维数组的表单:

<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]">

每个“成本”数组都有以下三个输入:描述,价格和文件上传 . 可能有多个“成本”数组(这就是我将第二个参数作为数字的原因) . 在我的CodeIgniter模型中,我有这个:

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);

'desc'和'price'的值进入数组(并获得序列化)完全没问题 - 但由于某种原因我无法接收任何文件信息 . 我删除了很多if语句,看看是不是问题,但事实并非如此 . 如果我更改了文件输入HTML标记,使其名称为“cost_1_file”,并且我将PHP模型更改为:

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

它回应了声明 - 这意味着它确保我的表格是 form_open_multipart . 有谁知道我哪里出错了?

1 回答

  • 2

    文件上传转到$ _FILES而不是$ _POST

相关问题