在codeigniter控制器中上传图像

时间:2014-12-31 11:31:44

标签: php image upload

我无法使用函数add()

中的代码行上传图像

为我提供一些上传图片的解决方案

公共功能add()     {

    if ($this->input->server('REQUEST_METHOD') === 'POST')
    {


        $this->form_validation->set_rules('image', 'image', 'required');
        $this->form_validation->set_rules('text', 'text', 'required');
        $this->form_validation->set_error_delimiters('<div class="alert alert-error"><a class="close" data-dismiss="alert">×</a><strong>', '</strong></div>');



        if ($this->form_validation->run())
        {

            **$data_to_insert = array(
                'image' => $this->input->post('image'),
                'text' => $this->input->post('text'),**

            );

            if($this->home_banner_model->insert_home_banner($data_to_insert)){
                $data['flash_message'] = TRUE; 
            }else{
                $data['flash_message'] = FALSE; 
            }

        }

    }

    $data['main_content'] = 'admin/home_banner/add';
    $this->load->view('includes_admin/template', $data);  
}       

1 个答案:

答案 0 :(得分:0)

   $config = array(
        'upload_path'   =>  'uploadPath',
        'allowed_types' =>  'jpg,jpeg',
        'max_size'      =>  1500
    );

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


        if(isset($_FILES['field_name']) && $_FILES[field_name]['name'] != NULL)

            if($this->upload->do_upload('field_name'))
            {
              $upload_data = $this->upload->data();

                $data_to_insert = array(
                    'attachment_link'   => 'uploadPath/'.$upload_data['file_name'],
                    'attachment_size'   => $upload_data['file_size']
                     // u can add parameters as for ur table to save
                    );

                $this->home_banner_model->insert_home_banner($data); 

            }
            else
            {

               echo $this->upload->display_errors();

            }