codeigniter - 如果图像字段为空,则上传图像显示错误

时间:2012-09-21 14:25:19

标签: codeigniter

我的控制器中有这个功能。如果我添加图像,它工作正常,但如果我不上传图像,我会收到以下错误:

消息:未定义的索引:categoryphoto 文件名:controllers / faqcategories.php 行号:90

这是我的代码

 public function addNewFaqCategory() 
    {    
        $currentUser = $this->isLoggedIn();
        $this->load->library('upload');
        $this->load->library('form_validation');
        $this->form_validation->set_rules('categoryname', 'Category Name', 'trim|required|xss_clean');
        $this->form_validation->set_rules('categoryname_en', 'Category Name', '');
        $this->form_validation->set_rules('visible', 'Visible', 'trim|required|xss_clean');
        $this->form_validation->set_rules('sorder', 'Sort Order', 'trim|numeric|xss_clean');

        if ($this->form_validation->run() == FALSE) 
        {
            $this->displayAddFaqCategoryForm();
        } else {

            $insertWhat = array(
                                'categoryname'    => $this->input->post('categoryname'),
                                'categoryname_en' => $this->input->post('categoryname_en'),
                                'parentid'        => $this->input->post('parentid'),
                                'description'     => $this->input->post('description'),
                                'description_en'  => $this->input->post('description_en'),
                                'metatags'        => $this->input->post('metatags'),
                                'metatags_en'     => $this->input->post('metatags_en'),
                                'sorder'          => $this->input->post('sorder'),
                                'visible'         => $this->input->post('visible'),
                                'categoryphoto'   => ($_FILES['categoryphoto']['name']) // line 90, error is here....

                               );

            if($insertWhat['categoryphoto'] !="")
            {
                $insertWhat['categoryphoto'] = str_replace(' ', '_',$insertWhat['categoryphoto']);
                $now = date('Y-m-d-His');
                $insertWhat['categoryphoto'] = $now.$insertWhat['categoryphoto'];
        $config['upload_path'] = 'backOffice/backOfficeImages';
        $config['allowed_types'] = 'gif|jpg|jpeg|bmp|png';
        $config['max_size'] = '2048';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
                $config['file_name'] = $insertWhat['categoryphoto'];
                $this->load->library('upload', $config);
                $this->upload->initialize($config);   
            }
            $this->upload->do_upload('categoryphoto');
            $data = array('upload_data' => $this->upload->data('categoryphoto'));
            $this->load->model('faqcategoriesmodel');
            $this->faqcategoriesmodel->save($insertWhat);
            $this->displayAllFaqCategories();
        }
    } // end of function addNewFaqCategory

第90行='categoryphoto'=> ($ _FILES [ 'categoryphoto'] [ '名称'])

任何帮助都将深受赞赏。

佐兰

1 个答案:

答案 0 :(得分:0)

在检查上传之前,您正在创建该阵列,因此如果没有上传照片,则会出错。您应该在检查/执行上传后移动数组创建

作为代码的一行修复,使用它来代替错误的行:

'categoryphoto'   => isset($_FILES['categoryphoto']['name'])? $_FILES['categoryphoto']['name'] : ''