无法上传图片codeigniter

时间:2016-04-22 11:02:26

标签: php codeigniter file-upload

我正在尝试上传图片。 以下是代码

<?php
$PerpetualCalendar = $this->input->post();
if($files['imgQuarter1']['name'][$key]!="")
{
    $_FILES['imgQuarter1']['name']= $files['imgQuarter1']['name'][$key];
    $_FILES['imgQuarter1']['type']= $files['imgQuarter1']['type'][$key];
    $_FILES['imgQuarter1']['tmp_name']= $files['imgQuarter1']['tmp_name'][$key];
    $_FILES['imgQuarter1']['error']= $files['imgQuarter1']['error'][$key];
    $_FILES['imgQuarter1']['size']= $files['imgQuarter1']['size'][$key];
    if(isset($PerpetualCalendar['id1'][$key]))
    {
        $img_id=$PerpetualCalendar['id1'][$key];
    }
    else
    {
        $img_id=$GetLastID;
    }
    $fileName = $img_id.'_NAME_'. $files['imgQuarter1']['name'][$key];
    $config['file_name'] = $fileName;
    $this->upload->initialize($config);
    if($this->upload->do_upload('imgQuarter1'))
    {
        if(isset($PerpetualCalendar['NAMEpath_idQ1'][$key])){
            $ImgUpdate[$imgkey]['tbl_name']='perpetual_calendar';
            $ImgUpdate[$imgkey]['field_name']='event_name';
            $ImgUpdate[$imgkey]['user_FK']=$user_id;
            $ImgUpdate[$imgkey]['path']=$uploadpath.'/'.$fileName;
            $ImgUpdate[$imgkey]['entry_id']=$img_id;
            $ImgUpdate[$imgkey]['path_id']=$PerpetualCalendar['NAMEpath_idQ1'][$key]; //for update purpose
        }else{

            $ImgInsert[$imgkey]['tbl_name']='perpetual_calendar';
            $ImgInsert[$imgkey]['field_name']='event_name';
            $ImgInsert[$imgkey]['user_FK']=$user_id;
            $ImgInsert[$imgkey]['path']=$uploadpath.'/'.$fileName;
            $ImgInsert[$imgkey]['entry_id']=$img_id;
        }
        $imgkey++;
    }
    else
    {
        $this->data['error'] = $this->session->set_flashdata(array('error' => $this->upload->display_errors()));
    }
}
?>

这是视图

echo form_upload(array('name'=>'imgQuarter1['.$key.']','class'=>'default'),'');
echo form_input(array('name' => 'id1['.$key.']', 'type'=>'hidden','value'=>$ValuePerpetualCalendar['cal_id']));

在上面的代码中,我可以获得 $ PerpetualCalendar [&#39; id1&#39;] [$ key] 此ID,但 图片无法在文件夹中上传,也无法保存在数据库中!

任何帮助?

1 个答案:

答案 0 :(得分:0)

  public function upload_multiple_image($user_id){

            $image = $_FILES['image']['name'];
            $files = $_FILES['image'];
            $image_title = $this->input->post('image_title');
            $image_link = $this->input->post('image_link');
            foreach($image as $key=>$data){
                if($files['name'][$key]!=""){               
                    $add_data['user_id'] = $user_id;
                    $add_data['title'] = $image_title[$key];
                    $add_data['description'] = $image_link[$key];
                    $add_data['status'] = 1;
                    $this->db->insert('user_images',$add_data);
                    $last_inserted_id = $this->db->insert_id();

                    $_FILES['image']['name'] = $files['name'][$key];
                    $_FILES['image']['type'] = $files['type'][$key];
                    $_FILES['image']['tmp_name'] = $files['tmp_name'][$key];
                    $_FILES['image']['error'] = $files['error'][$key];
                    $_FILES['image']['size'] = $files['size'][$key];

                    $config['upload_path'] = './user_images/original/';
                    $config['allowed_types'] = 'gif|jpg|png|jpeg';
                    $config['overwrite'] = TRUE;
                    $config['file_name'] = 'place_image_'.$last_inserted_id;

                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);

                    if($this->upload->do_upload('image')){
                        $file_details = $this->upload->data();
                        $update_data = array('image'=>$file_details['file_name']);
                        $this->db->update('user_images',$update_data,array('id'=>$last_inserted_id));
                    }
                }
            }


        }