Codeigniter图片上传问题

时间:2017-02-12 15:12:19

标签: php codeigniter upload

我正在开发codeigniter项目。一切正在我的本地主机上工作。现在我已经将所有的东西都转移到了服务器上我也在使用 cloudfare

问题是,当我尝试上传图片时,它显示我的空白屏幕。以下是我的控制器代码。

// Add Data
    function add(){
        $data['pageTitle']=$this->lang->line('siteTitle');
        // Breadcrumbs
        $this->breadcrumbs->push('Dashboard','admin/dashboard');
        $this->breadcrumbs->push('Category List','admin/category/index');
        $this->breadcrumbs->push('Add Category','admin/category/add');
        // Load View
        // Form Submit
        $this->form_validation->set_rules('_cat_title','Title','required|is_unique[pu_project_category.c_title]');
        $this->form_validation->set_rules('_cat_slug','Slug','required');
        $this->form_validation->set_rules('_cat_desc','Description','required');
        if($this->form_validation->run()==TRUE){
            if($this->upload->do_upload('_cat_image')){
                $image=$this->upload->data();
                $image=$image['file_name'];
            }else{
                $image='No Image';
            }
            $data['resUpload']=$this->upload->display_errors();
            $data['res']=$this->Category_Model->add($image);
        }
        // Load View
        $this->load->view('admin/common/header',$data);
        $this->load->view('admin/common/left-sidebar',$data);
        $this->load->view('admin/category/add',$data);
        $this->load->view('admin/common/footer',$data);
    }

以下是上传文件的配置代码,

// Upload Configuration
$config['upload_path']          = './project_images/';
$config['allowed_types']        = 'gif|jpg|png|jpeg|zip';
$config['max_size']             = '2000';
$this->load->library('upload', $config);
$this->upload->initialize($config);

请帮我解决这个问题并告诉我哪里出错了。

1 个答案:

答案 0 :(得分:0)

试试这个

    public function add()
{



      $this->form_validation->set_rules('_cat_slug','Slug','required');

   if($this->form_validation->run() == TRUE)
            {
                   $this->load->model('welcome_model');//change this with your model



               //image upload
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'png|gif|jpg|jpeg';
                $config['max_size']    = '7000';
                $this->load->library('upload',$config);
                if ( ! $this->upload->do_upload('image'))
                {
                   $error = array('error' => $this->upload->display_errors());
                    $this->load->view('upload_view', $error);  //loads the view display.php with error
                }

                $upload_data=$this->upload->data();

                $image=$upload_data['file_name'];

                $this->load->view('upload_view');

                     $data= array(
                    '_cat_slug' => $this->input->post('_cat_slug'),
                    'image' => $image

                ); //loads view display.php with data
               $this->welcome_model->registre($data);//change this with your model function name
            }


            else
            {  

            echo validation_errors();


            }

}