1

I'm trying to create a custom exception to return a correct message based on $_FILES[..]['error'] codes.

I have extended the 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;
    }    
}

But when I call file_upload_error(1) it says Call to undefined function file_upload_error()

How do I approach this problem?

2
  • 1
    Your function is defined outside your class? Commented Apr 22, 2013 at 11:01
  • 1
    Show us where and how you call this class method. Commented Apr 22, 2013 at 11:06

1 Answer 1

2

I get the feeling that you're not using your library class correctly, but it's hard to tell from your question. From a model or controller class, you would use:

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

when you tell it to load the exceptions library, it automatically looks to see if there's one in your application/libraries/ directory with the MY_ prefix. If it's there, it will load it. Then you access it as with any other library.

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.