Php 在模型中调用时,Codeigniter无法加载上载库
我在$this->load->library('upload')上遇到了麻烦;如果我在控制器中调用它,它可以正常工作,但是当我将它放入模型中时,它会给我一个错误“调用非对象上的成员函数do_upload()” 这是我的上传代码Php 在模型中调用时,Codeigniter无法加载上载库,php,codeigniter,upload,Php,Codeigniter,Upload,我在$this->load->library('upload')上遇到了麻烦;如果我在控制器中调用它,它可以正常工作,但是当我将它放入模型中时,它会给我一个错误“调用非对象上的成员函数do_upload()” 这是我的上传代码 function upload_file_model(){ if(isset($_FILES['upPhoto'])){ $upMsg = ""; $uploadPath; $uploadDirectory;
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();
}
函数上传文件模型(){
如果(isset($_文件['upPhoto'])){
$upMsg=“”;
$uploadPath;
$uploadDirectory;
$this->uploadPath=realpath(APPPATH.DIRECTORY_SEPARATOR.“root”);
$this->uploadDirectory=$this->input->post('txt\u current\u directory');
$this->uploadDirectory=explode(“/”,$this->uploadDirectory);
$this->uploadDirectory=内爆(目录分隔符,$this->uploadDirectory);
$this->upload=$this->uploadPath.DIRECTORY\u分隔符。$this->uploadDirectory;
对于($i=0;$i$this->上载,
“允许的_类型”=>“gif | jpg | png | jpeg | doc | csv”,
“最大尺寸”=>“10000”,
“最大宽度”=>“10000”,
“最大高度”=>“10000”,
“覆盖”=>false,
'文件名'=>'测试'+$i
);
$this->load->library('upload',$config);
如果($this->upload->do_upload()){
$this->upMsg.=$\u文件['upPhoto']['name'][$i].“已上载。
”;
}否则{
$this->upMsg.=$\u文件['upPhoto']['name'][$i].”未上载。
.ERROR:“.$this->upload->display_errors();
}
}
//echo$this->upMsg;
$this->status=TRUE;
$this->msg=$this->upMsg;
}否则{
//呼应“假”;
$this->status=FALSE;
$this->msg=“错误:您没有选择要上载的文件。”;
}
返回$this->respond();
}
大部分代码来自phpx的链接
我只是修改了一些 当您不在控制器内时,可以使用以下命令获取
$this
对象:
get_instance();
因此,您可以执行以下操作从模型(或任何地方)加载上载库:
为什么要在
for
循环之前将$this->upload
分配给字符串?@KemalFadillah,我怎么会错过这个!我更改了$this->upload,它成功了。谢谢你,嗯…什么时候开始的?我从来没见过…是哪个版本?另外,对superobject的$this引用也存在于模型中。嗯,我不确定我是否在CI文档中读到了它?这是我第一次听说它。你能进一步解释一下吗?thanks@jONGsKiE777您不需要这些,在控制器、模型和视图中使用$this可以很好地工作。只有在库中,您才需要创建对CI实例的引用,可能该函数是执行此操作的自定义函数,但是$CI=get_instance()$ci->load->
是最常见的道歉方式,函数是get\u instance()
。我已经更新了我的示例ci()
是我熟悉的同一种方法的缩写。
get_instance()->load->library('upload', $config);