使用CodeIgniter Upload类,尝试将选择数据放入数据库存储的新数组中

时间:2013-11-14 23:19:09

标签: php arrays codeigniter

我正在尝试使用CodeIgners Upload类从成功上传的图像中获取信息(file_name,image_size_str)。我想把这个选择数据放入一个新数组,然后我可以存储在数据库表中。使用foreach循环数据时,代码失败,尝试使用array_push进入新数组。我不得不评论。

function do_upload(){
$dbImgInfoStore = array();  
$this->load->model('upload_model');
$config['upload_path']='./uploads/';
$config['allowed_types']='gif|jpg|png';
$config['max_size']='10000'; //etc, etc
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
    $error = array('error'=>$this->upload->display_errors());
    $this->load->view('upload_form',$error);
} else {
    $data = array('upload_data'=>$this->upload->data());
    // NOT WORKING EITHER! 
    foreach($data as $key=>$value) {
        if($key == 'image_size_str') {
            array_push($dbImgInfoStore, $dbImgInfoStore['image_size_str']=$value);
        } elseif ($key == 'file_name') {
            array_push($dbImgInfoStore, $dbImgInfoStore['file_name']=$value);
        }
    } 
    $this->load->view('upload_success', $data);

}
}
奇怪的是,发送到新页面时

$this->load->view('upload_success', $data);

我能够完成我之前尝试做几行的事情。

1 个答案:

答案 0 :(得分:2)

您需要访问$data['upload_data']循环中的foreach

foreach($data['upload_data'] as $key => $value) {
    if($key == 'image_size_str') {
       $dbImgInfoStore['image_size_str'] = $value;
    } 
    elseif ($key == 'file_name') {
       $dbImgInfoStore['file_name'] = $value;
    }
}