简体   繁体   English

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

[英]Codeigniter unable to load upload library when called in model

Im having trouble with the $this->load->library('upload'); 我在$ 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" 如果我在控制器中调用它,就可以正常工作,但是当我将其放入模型中时,会出现错误“在非对象上调用成员函数do_upload()”

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. 大多数代码来自此链接Codeigniter多个文件上传混乱文件扩展名 〜by phpx我刚刚修改了其中一些。

When you're not inside a controller you can use the following to get the $this object: 当您不在控制器内部时,可以使用以下命令获取$this对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM