0

Im having trouble with the $this->load->library('upload'); if i call it in the controller it works just fine but when i put it inside a model it gives me an error " Call to a member function do_upload() on a non-object"

Here's my upload code.

function upload_file_model(){
    if(isset($_FILES['upPhoto'])){
        $upMsg = "";
        $uploadPath;
        $uploadDirectory;
        $this->uploadPath = realpath(APPPATH . DIRECTORY_SEPARATOR . "root");
        $this->uploadDirectory = $this->input->post('txt_current_directory');
        $this->uploadDirectory = explode("/", $this->uploadDirectory);
        $this->uploadDirectory = implode(DIRECTORY_SEPARATOR, $this->uploadDirectory);
        $this->upload = $this->uploadPath . DIRECTORY_SEPARATOR . $this->uploadDirectory;

        for($i = 0;$i<count($_FILES['upPhoto']['name']);$i++){
            $_FILES['userfile']['name'] = $_FILES['upPhoto']['name'][$i];
            $_FILES['userfile']['type'] = $_FILES['upPhoto']['type'][$i];
            $_FILES['userfile']['tmp_name'] = $_FILES['upPhoto']['tmp_name'][$i];
            $_FILES['userfile']['error'] = $_FILES['upPhoto']['error'][$i];
            $_FILES['userfile']['size'] = $_FILES['upPhoto']['size'][$i];
            $config = array(
                'upload_path' => $this->upload,
                'allowed_types' => 'gif|jpg|png|jpeg|doc|csv',
                'max_size' => '10000',
                'max_width' => '10000',
                'max_height' => '10000',
                'overwrite' => false,
                'file_name' => 'test_' + $i
            );

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

            if($this->upload->do_upload()){
                $this->upMsg .= $_FILES['upPhoto']['name'][$i] . " Uploaded. <br>"; 
            }else{
                $this->upMsg .= $_FILES['upPhoto']['name'][$i] . " was not Uploaded. <br> . ERROR : " . $this->upload->display_errors();
            }
        }
        // echo $this->upMsg;
        $this->status = TRUE;
        $this->msg = $this->upMsg;
    }else{
        // echo "FALSE";
        $this->status = FALSE;
        $this->msg = "ERROR : You did not select a file to upload."; 
    }
    return $this->respond();
}

Most of the code comes from this link Codeigniter multiple file upload messes file extension ~by phpx i just modified some of it.

Community
  • 1
  • 1
jONGsKiE777
  • 156
  • 3
  • 7

1 Answers1

0

When you're not inside a controller you can use the following to get the $this object:

get_instance();

So you can do the following to load the upload library from a model (or anywhere really):

get_instance()->load->library('upload', $config);
Matt
  • 9,068
  • 12
  • 64
  • 84
  • Uhm...since when? I never saw that...Which version is it? ALso, the $this reference to the superobject exists in models too – Damien Pirsy Feb 06 '12 at 15:54
  • hmm, im not sure if i read that in the CI Documentation ? this is the first time i heard of it. can you further explain ? thanks – jONGsKiE777 Feb 06 '12 at 16:12
  • @jONGsKiE777 You don't need that, using $this works fine in controllers, models and views. Only in libraries you need to create a reference to the CI instance, maybe that function is a custom one that does this, but `$ci = get_instance(); $ci->load->` is the most common way – Damien Pirsy Feb 06 '12 at 16:33
  • Apologies, the function is `get_instance()`. I've updated my example. `ci()` is a shorthand to the same method that I'm familiar with. – Matt Feb 06 '12 at 16:33