这是我第一次来这里。我真的不知道我的代码有什么问题。我试图将文件上传到数据库但是当我点击上传按钮时,会发生错误..(找不到对象)希望有人可以帮助我......
btw,继承我的代码片段
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|doc|txt|pdf';
$config['max_size'] = '5000';
$config['max_width'] = '2500';
$config['max_height'] = '2500';
$config['remove_spaces']= 'true';
$this->load->library('upload', $config);
$data= array('userfile' => $this->upload->do_upload());
//DEFINE POSTED FILE INTO VARIABLE
$name= $data['userfile']['name'];
$tmpname= $data['userfile']['tmpname'];
$type= $data['userfile']['type'];
$size= $data['userfile']['size'];
//OPEN FILE AND EXTRACT DATA /CONTENT FROM IT
$fp = fopen($tmpname, 'r');
$content= fread($fp, $size($tmpname));
$content= addslashes($content);
fclose($fp);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('userfile' => $this->upload->data());
$this->load->database();
$ret=$this->db->insert($name, $type, $size, $content);
unlink($tmpname);
//$this->load->database();
//$this->load->view('upload', $data);
}
}
答案 0 :(得分:0)
$this->db->insert()
方法无法正常工作。它需要两个参数:第一个是要插入数据的表,第二个是包含数据的数组。
在您的情况下,您应该首先将文件的数据放入数组中:
$file_data=array('name'=>$name,'type'=>$type,'size'=>$size,'content'=>$content)
然后将其插入相应的表中。我以files
为例。使用你真正需要的那个。
$ret=$this->db->insert('files',$file_data);
请注意,除了在极少数情况下(文件写入禁止等等),通常最好将文件保存为...文件