2013-09-28 45 views
0

我提到@raheel上this page山的線程,並使用該My_Upload擴展庫中的輸入文件的if(!$this->upload->validate_upload('photo'))驗證時,驗證是正常工作,如果用戶嘗試上傳無效的文件類型和信息只顯示在正確的地方。笨validate_upload庫不能上傳文件

不知何故,我無法將圖像文件上傳到它的文件夾,並且文件名無法保存到表格中,在使用此庫之前,上傳圖像文件並保存到表格的工作非常漂亮。

有人可以請幫助圖我的代碼有什麼問題嗎?

控制器:

public function index() 
{ 
    if(!$this->tank_auth->is_logged_in()) { 
     redirect('/auth/login'); 

    }else{ 

     $user_id = $this->session->userdata('user_id'); 
     $profile = $this->users->get_profile_by_id($user_id); 
     $checked = $this->input->post('remove'); 

     $this->form_validation->set_rules('name', 'Name', 'trim|required|xss_clean|min_length[5]|max_length[30]'); 
     $this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean'); 
     $this->form_validation->set_rules('website', 'Website', 'trim|xss_clean|max_length[30]'); 


     if(isset($_FILES['photo']) AND !empty($_FILES['photo']['name'])) { 

      $this->form_validation->set_rules('photo', 'Photo', 'trim|callback_valid_upload'); 

      if($this->upload->do_upload('photo')){ 

       // image resizing 
       $config['source_image'] = $this->upload->upload_path.$this->upload->file_name; 
       $config['maintain_ratio'] = TRUE; 
       $config['width'] = 100; 
       $config['height'] = 100; 

       $this->load->library('image_lib', $config); //codeigniter default function 

       if(!$this->image_lib->resize()){ 

        $this->session->set_flashdata('upload_msg', $this->image_lib->display_errors()); 

       }else{ 

        $image_data = $this->upload->data(); 
        $photo_to_table = $image_data['file_name']; 
       } 

      } 

     }else{ 

      if((int) $checked == 1){ 

       unlink('./uploads/'.$profile->photo); 
       $photo_to_table = ''; 

      }else{ 
       $photo_to_table = $profile->photo; 
      } 
      //$photo_to_table = ((int) $checked == 1) ? '' : $profile->photo; 
     } 

     if($this->form_validation->run()) { // validation ok 

      if(!is_null($data = $this->tank_auth->update_user(
       $this->form_validation->set_value('name'), 
       $this->form_validation->set_value('country'), 
       $this->form_validation->set_value('website'), 
       $photo_to_table 
      ))) { // success 

       $this->session->set_flashdata('msg', 'Profile has been updated'); 
       redirect(current_url()); 
      } 

     }else{ 

      $errors = $this->tank_auth->get_error_message(); 
      foreach($errors as $k => $v) $data['errors'][$k] = $this->lang->line($v); 
     } 

//blah blah..//load view 

} 

public function valid_upload() 
{ 
    $user_id = $this->session->userdata('user_id'); 

    $config['upload_path'] = './uploads/'; 
    $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config['max_size'] = '500'; 
    $config['file_name'] = $user_id.'_'.strtotime('now'); 
    $config['overwrite'] = TRUE; 
    $config['remove_spaces'] = TRUE; 

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

    if(!$this->upload->validate_upload('photo')) 
    { 
     $this->form_validation->set_message('valid_upload', $this->upload->display_errors()); 
     return FALSE; 
    }else{ 
     return TRUE; 
    } 
} 


} 

感謝。

回答

1

我有一個類似的問題,在我的情況下,這是因爲我選擇了錯誤的圖像庫。檢查您的PHP安裝是否需要使用適當的圖像庫,並確保它匹配。默認情況下,CodeIgniter選擇GD2(GD庫版本> = 2)作爲默認圖像庫。
http://ellislab.com/codeigniter/user-guide/libraries/image_lib.html

試試這個:

// image resizing 
$config['image_library'] = 'gd'; // specify the correct image library 
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name; 
$config['maintain_ratio'] = TRUE; 
$config['width'] = 100; 
$config['height'] = 100; 
0

你是什麼結果

$this->image_lib->display_errors() 

是您的上傳目錄寫嗎?

你有GD安裝和活躍在PHP?也許嘗試馬哈茂德建議的GD?