我们正在尝试使用Codeigniter上传一些文件,但出于某种原因,这段代码无效。
将图书馆加载到" autoload"由于显而易见的原因,它仍然不是最佳解决方案。
奇怪的是,我正在另一个网站工作。
<?php
// set options
$config['upload_path'] = FCPATH.$path;
$config['allowed_types'] = 'png|jpeg|jpg|gif';
$config['max_size'] = 2048;
$config['encrypt_name'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_width'] = 0;
$config['max_height'] = 0;
// check if folder exists
if( ! is_dir($config['upload_path']))
@mkdir($config['upload_path'], 0755, true);
//load upload library
$this->load->library('upload', $config);
//check success of upload
if( ! $this->upload->do_upload($name))
return $this->upload->display_errors();
else
{
// do upload
}
?>
这是错误:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: xxx::$upload
Filename: core/xxx.php
Line Number: 424
Fatal error: Call to a member function do_upload() on null
答案 0 :(得分:1)
首先确保$path
是正确的var dump($ path)并检查是否接收它。
其次,如果do_upload的一部分和is目录周围没有任何{}
public function do_upload() {
$config['upload_path'] = FCPATH . 'uploads/';
$config['allowed_types'] = 'png|jpeg|jpg';
$config['max_size'] = 2048;
$config['encrypt_name'] = TRUE;
$config['max_size'] = '30000'; // Added Max Size
$config['overwrite'] = TRUE;
$config['max_width'] = 0;
$config['max_height'] = 0;
//load upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
// check if folder exists
if( ! is_dir($config['upload_path'])) {
@mkdir($config['upload_path'], 0755, true);
}
//check success of upload
$name = "userfile"; // Field name
if( ! $this->upload->do_upload($name)) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
确保在视图中使用echo form_open_multipart('controller/do_upload')
http://www.codeigniter.com/userguide2/libraries/file_uploading.html