在模型中调用时,Codeigniter无法加载上载库

时间:2012-02-06 15:33:49

标签: php codeigniter upload

我在使用$ this-> load->库时遇到问题('upload');如果我在控制器中调用它它工作得很好但是当我把它放在模型中它给我一个错误“调用非对象上的成员函数do_upload()”

这是我的上传代码。

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();
}

大部分代码来自phpx的这个链接Codeigniter multiple file upload messes file extension〜 我只修改了一些。

1 个答案:

答案 0 :(得分:0)

如果您不在控制器内,可以使用以下内容获取$this对象:

get_instance();

因此,您可以执行以下操作从模型(或任何地方)加载上传库:

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