2013-04-22 55 views
1

我想創建一個自定義異常,以基於$_FILES[..]['error']代碼返回正確的消息。CodeIgniter創建新的自定義異常

我已經擴展了CI_Exceptions class

class MY_Exceptions extends CI_Exceptions { 
    function My_Exceptions() { 
     parent::CI_Exceptions(); 
    } 

    public function file_upload_error($error_code = UPLOAD_ERR_OK) { 
     switch ($error_code) { 
      case UPLOAD_ERR_INI_SIZE: 
       $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; 
       break; 
      case UPLOAD_ERR_FORM_SIZE: 
       $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"; 
       break; 
      case UPLOAD_ERR_PARTIAL: 
       $message = "The uploaded file was only partially uploaded"; 
       break; 
      case UPLOAD_ERR_NO_FILE: 
       $message = "No file was uploaded"; 
       break; 
      case UPLOAD_ERR_NO_TMP_DIR: 
       $message = "Missing a temporary folder"; 
       break; 
      case UPLOAD_ERR_CANT_WRITE: 
       $message = "Failed to write file to disk"; 
       break; 
      case UPLOAD_ERR_EXTENSION: 
       $message = "File upload stopped by extension"; 
       break; 

      default: 
       $message = "Unknown upload error"; 
       break; 
     } 

     log_message('error', $error_code); 
     return $message; 
    }  
} 

但是當我打電話file_upload_error(1)它說:Call to undefined function file_upload_error()

我該如何解決這個問題?

+1

你的功能是在課堂外定義的嗎? – 2013-04-22 11:01:08

+1

向我們展示您在何處以及如何調用此類方法。 – karmafunk 2013-04-22 11:06:18

回答

2

我覺得你沒有正確使用你的類庫,但很難從你的問題中知道。從模型或控制器類,你可以使用:

$this->load->library('exceptions'); 
$message = $this->exceptions->file_upload_error($error_code); 

當你告訴它加載例外庫,它會自動查看是否有一個與MY_前綴您的應用程序/庫/目錄。如果它在那裏,它會加載它。然後,您可以像訪問其他庫一樣訪問它。

希望有所幫助。