【问题标题】:Problems with CodeIgniter file uploads and multi-dimensional arraysCodeIgniter 文件上传和多维数组的问题
【发布时间】:2011-07-28 09:09:16
【问题描述】:

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

<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。有人知道我哪里出错了吗?

【问题讨论】:

  • 查看页面 roytuts.com/codeigniter-multiple-files-upload/

标签: php codeigniter file-upload multidimensional-array


【解决方案1】:

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

【讨论】:

  • 发帖5分钟后我就意识到了,哈哈。谢谢!
猜你喜欢
  • 1970-01-01
  • 2017-09-29
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
相关资源
最近更新 更多