如何在codeigniter中使用多个图像

时间:2017-05-24 13:05:09

标签: codeigniter

在我的页面中,我希望用户选择多个图像并上传它我将图像名称保存在数据库中以供参考。我成功上传了数据库中的单个图像,也可以在视图中显示图像,但现在我在上传多个图像时遇到了问题。

public function add_record()
{
    $this->form_validation->set_rules('category', 'category', 'required');
    $current_date = date("Y-m-d H:i:s");
    $error='';

    if($this->form_validation->run())
    {
        $image = '';
        if($_FILES['image']['name'])
        {
            if (!is_dir('/backend_assets/media/image/')) {
                mkdir('./backend_assets/media/image/', 0777, TRUE);

            }
            $config['upload_path']          = './backend_assets/media/image/';
            $config['allowed_types']        = 'gif|jpg|jpeg|png';
            $this->load->library('upload', $config);
            $this->upload->initialize($config);             
            if ($this->upload->do_upload('image'))
            {
                $data = $this->upload->data();
                $image = $data['file_name'];

            }else{
                    $this->session->set_flashdata('error', $this->upload->display_errors());
                    redirect(base_url('admin/image')); 
            }

        }

        $insert_array = array(
                                'gl_cat_id' => $this->input->post('category'),
                                'gl_image'=> $image                         
                            );
        if ($this->common_model->add_records('vm_image',$insert_array))
            {
                $id = $this->db->insert_id();
                $insert_sco_details = array(
                                    'sd_ty'=>'vm_image',
                                    'sd_ty_id'=>$id,
                                    'sd_image'=>$image
                                     );
                if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
                {
                    $this->session->set_flashdata('success','Record added successfully');
                    redirect(base_url('admin/image'));
                }else{
                    $this->session->set_flashdata('error','Error while adding record');
                    redirect(base_url('admin/image')); 
                }

            }else{
                $this->session->set_flashdata('error','Error while adding record');
                redirect(base_url('admin/image')); 
            }

    }
    $where_array = array('vm_publications.status !=' => 4);
    $data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
    $data['include'] = 'backend/image/add_image';
    $this->load->view('backend/container', $data);
}

上述代码怎么可能??

3 个答案:

答案 0 :(得分:1)

$current_date = date("Y-m-d H:i:s");
    $error = '';
    $image = '';
    if(isset($_FILES['image']['name']))
    {
        //print_r($_FILES);
        $id = base64_decode($this->input->post('gid'));

        $filesCount = count($_FILES['image']['name']);
        $inserted = '';
        for($i = 0; $i < $filesCount; $i++)
        {
            $_FILES['userFile']['name'] = $_FILES['image']['name'][$i];
            $_FILES['userFile']['type'] = $_FILES['image']['type'][$i];
            $_FILES['userFile']['tmp_name'] = $_FILES['image']['tmp_name'][$i];
            $_FILES['userFile']['error'] = $_FILES['image']['error'][$i];
            $_FILES['userFile']['size'] = $_FILES['image']['size'][$i];

            $config['upload_path']          = './backend_assets/media/image/';
            $config['allowed_types']        = 'gif|jpg|png';

            $this->load->library('upload', $config);
            $this->upload->initialize($config);
            if($this->upload->do_upload('userFile'))
            {
                $fileData = $this->upload->data();
                $image = $fileData['file_name'];
                $insert_array = array(
                                        'gl_cat_id' => $this->input->post('category'),
                                        'gl_image'=> $image                         
                                    );

            if ($this->common_model->add_records('vm_image',$insert_array))
            {
                $id = $this->db->insert_id();
                $insert_sco_details = array(
                                            'sd_ty'=>'vm_image',
                                            'sd_ty_id'=>$id,
                                            'sd_image'=>$image
                                             );
                if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))   
                {
                    $inserted++;
                }

            }
        }
        }
       if($inserted == $filesCount)
        {   $this->session->set_flashdata('success','Images uploaded successfully');
                redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
        }else{
            $this->session->set_flashdata('error','Error while adding record');
            redirect(base_url('adminp8AamG6ueHFNGAAp/image')); 
        }
    }
    $where_array = array('vm_publications.status !=' => 4);
    $data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
    $data['include'] = 'backend/image/add_image';
    $this->load->view('backend/container', $data);

答案 1 :(得分:0)

在add_record()函数中尝试下面的代码。它会对你有所帮助。几天前,我遇到了同样的问题

$files = $_FILES;
        $count = count($_FILES['image']['name']);
        for($i=0; $i<$count; $i++)  {
                $_FILES['image']['name']= $files['image']['name'][$i];
                $_FILES['image']['type']= $files['image']['type'][$i];
                $_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
                $_FILES['image']['error']= $files['image']['error'][$i];
                $_FILES['image']['size']= $files['image']['size'][$i];
                $this->upload->initialize($this->set_upload_options());//function defination below
                $this->upload->do_upload('image');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;

       }

      $fileName = $images;

并在同一控制器的set_upload_options函数中设置文件上载配置。

function set_upload_options() { 
         $config = array();
         $config['upload_path'] = PATH;
         $config['remove_spaces']=TRUE;
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         return $config;
}

答案 2 :(得分:-1)

首先,调试$_FILES变量的输出。这应该会为您提供一系列正在上传的文件。循环通过它们来单独处理每一个。

foreach ($_FILES as $file) { // do some file processing on the $file object instead of $_FILES object. // example: instead of using 'if($_FILES['image']['name'])' // use: 'if($file['image']['name'])' }

如果您想使用CI的上传课程,请在此处查看他们的文档:https://www.codeigniter.com/userguide3/libraries/file_uploading.html