phpcodeigniter

codeigniter - upload image display error if image field is empty


I have this function in my controller. If I add an image, it works fine, but if I don't upload an image, I get the following error:

Message: Undefined index: categoryphoto Filename: controllers/faqcategories.php Line number:90

Here is my code

 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

Line 90 = 'categoryphoto' => ($_FILES['categoryphoto']['name'])

Any help will be deeply appreciated.

Zoran


Solution

  • You are creating that array before checking the upload, so it will error out if there is no photo uploaded. You should move the array creation after checking/doing the upload

    As a one line fix for your code, use this in place of the faulty line:

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