2012-09-21 74 views
0

我在我的控制器中有這個功能。如果我添加圖像,它工作正常,但如果我不上傳圖片,我得到以下錯誤:codeigniter - 如果圖像字段爲空,上傳圖像顯示錯誤

消息:未定義指數:categoryphoto 文件名:控制器/ 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'] [ '名'])

任何幫助將十分讚賞。

卓然

回答

0

你正在創建檢查上傳之前陣列,因此它會報錯了,如果沒有照片上傳。你應該檢查後移動數組創建/做上傳

這樣一行修復您的代碼,而故障線路的使用:現在

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

就像一個魅力。謝謝Silviu – Zoran

+0

很高興我能幫忙 –