2012-07-09 106 views
0

在我的Codeigniter控制器中有以下私有函數驗證文件上傳。從另一個函數的私人函數返回FALSE

 private function avatar_file_validation() 
    { 
     $config['upload_path'] = './uploads/avatars/'; 
     $config['allowed_types'] = 'jpg|png'; 
     $config['overwrite'] = TRUE; //overwrite user avatar 
     $config['max_size'] = '800'; //in KB 

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

     if (! $this->upload->do_upload('avatar_upload')) 
     { 
      $error_data = array('error' => $this->upload->display_errors()); 

      $this->avatar_view($error_data); //loads view 

      return FALSE; 
     } 

    } 

如果在上載時發生的錯誤,我想從持續

function upload_avatar() 
{ 

    //some code 

    if($_FILES['entry_upload']['error'] !== 4) //if file added to file field 
    { 
     $this->avatar_file_validation(); //if returns FALSE stop code 
    } 

    //code continues: adds data to database, redirects 

} 

然而函數返回false,如果繼續甚至停止此功能。它只適用於我在1函數中使用整個代碼,但我需要將它們分開,因爲我將在多個函數中使用上傳驗證。我在這裏做錯了什麼?

+1

使用return語句?返回$ this-> avatar_file_validation()。它會停止執行該功能 – 2012-07-09 19:08:57

回答

2

表達式return FALSE;僅適用於功能avatar_file_validation()。如果您想在上傳失敗時停止upload_avatar()中的代碼,則應該檢查avatar_file_validation()的輸出,如果它等於FALSE,則還應該從該函數返回。

例如:

function upload_avatar() 
{ 
    //some code 

    if($_FILES['entry_upload']['error'] !== 4) //if file added to file field 
    { 
     if(!$this->avatar_file_validation()) //if returns FALSE stop code 
      return FALSE; 
    } 

    //code continues: adds data to database, redirects 
} 
2
function upload_avatar() 
{ 

    //some code 

    if(!$_FILES['entry_upload']['error'] !== 4) //if file added to file field 
    { 
     if($this->avatar_file_validation()){ 
      return FALSE; 
     } 
    } 

    //code continues: adds data to database, redirects 

}