以 blob 格式将文件插入数据库 table
Inserting a file into database table in blob format
我正在使用 codeigniter 框架,我正在尝试将 blob 内容(在本例中为图像)上传到数据库表中
以下是我的控制器代码
public function usersubmit()
{
$this->form_validation->set_rules('name','Name','trim|required|min_length[5]|max_length[255]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password','Password','required|min_length[8]|max_length[255]|trim|matches[passconf]|md5');
$this->form_validation->set_rules('passconf','Password','required|trim');
$this->form_validation->set_rules('phone','Mobile No.','trim|less_than[10000000000]');
$this->form_validation->set_rules('city','City','trim');
$this->form_validation->set_rules('pincode','Pincode','trim|numeric');
/*$config['upload_path']='./files/images/profilepic/';
$config['allowed_types']='gif|jpg|png|jpeg';
$config['max_size']='16000';
//$config['file_name']=''
//$this->load->library('upload',$config);
$this->upload->initialize($config);*/
if(!$this->form_validation->run())
{
$data['title']="Users";
$data['page']="createuser";
$this->load->view('main',data);
return;
}
$content;
$tmpname = $_FILES['propic']['tmp_name']; //The temporary filename of the file in which the uploaded file was stored on the server.
$filesize = $_FILES['propic']['size'];
$filetype = $_FILES['propic']['type'];
$allowedtypes=array("image/jpeg","image/jpg","image/png","image/gif");
if($filesize>=0){
if(in_array($filetype, $allowedtypes))
{
$fp = fopen($tmpname, 'r');
$content = fread($fp, filesize($tmpname));
$data['photo'] = addslashes($content); //it adds blackslashes after each quote(double or single)
fclose($fp);
}
}
//$fieldname='propic';
$data['name']= $this->input->post('name');
$data['email']= $this->input->post('email');
$data['password']=$this->input->post('password');
$data['phone']=$this->input->post('phone');
$data['city']=$this->input->post('city');
$data['pincode']= $this->input->post('pincode');
$data['dob']=$this->input->post('dob');
$data['id']=NULL;
$data['accesslevel']='2';
$data['status']='1';
$data['timestamp']=NULL;
if($this->user_model->createuser($data))
{
$this->viewusers();
}
else
{
$this->createuser();
}
//$data
}
以下是使用的模型函数
public function createuser($user)
{
$query= $this->db->insert('user',$user);
if($query)
return TRUE;
return false;
}
和视图中的输入表单
<form role="form" method="post" enctype="multipart/form-data" action="<?php echo site_url('start/usersubmit') ;?>">
<input type="file" class="form-control" name="propic" id="propic" />
<input type="submit" value="submit" name="submit"/>
</form>
除上传的文件外,此处一切正常。
该文件已上传到数据库中,但大小略大于实际文件(大约多 5%),而且文件已损坏。那就是当我从数据库下载它时,文件不可读。我需要知道我的代码有什么错误。
根据聊天中的讨论,这是解决方案:
替换以下代码:
$content = fread($fp, filesize($tmpname));
$data['photo'] = addslashes($content); //it adds blackslashes after each quote(double or single)
与:
$content = fread($fp, filesize($tmpname));
$data['photo'] = $content;
我正在使用 codeigniter 框架,我正在尝试将 blob 内容(在本例中为图像)上传到数据库表中
以下是我的控制器代码
public function usersubmit()
{
$this->form_validation->set_rules('name','Name','trim|required|min_length[5]|max_length[255]|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password','Password','required|min_length[8]|max_length[255]|trim|matches[passconf]|md5');
$this->form_validation->set_rules('passconf','Password','required|trim');
$this->form_validation->set_rules('phone','Mobile No.','trim|less_than[10000000000]');
$this->form_validation->set_rules('city','City','trim');
$this->form_validation->set_rules('pincode','Pincode','trim|numeric');
/*$config['upload_path']='./files/images/profilepic/';
$config['allowed_types']='gif|jpg|png|jpeg';
$config['max_size']='16000';
//$config['file_name']=''
//$this->load->library('upload',$config);
$this->upload->initialize($config);*/
if(!$this->form_validation->run())
{
$data['title']="Users";
$data['page']="createuser";
$this->load->view('main',data);
return;
}
$content;
$tmpname = $_FILES['propic']['tmp_name']; //The temporary filename of the file in which the uploaded file was stored on the server.
$filesize = $_FILES['propic']['size'];
$filetype = $_FILES['propic']['type'];
$allowedtypes=array("image/jpeg","image/jpg","image/png","image/gif");
if($filesize>=0){
if(in_array($filetype, $allowedtypes))
{
$fp = fopen($tmpname, 'r');
$content = fread($fp, filesize($tmpname));
$data['photo'] = addslashes($content); //it adds blackslashes after each quote(double or single)
fclose($fp);
}
}
//$fieldname='propic';
$data['name']= $this->input->post('name');
$data['email']= $this->input->post('email');
$data['password']=$this->input->post('password');
$data['phone']=$this->input->post('phone');
$data['city']=$this->input->post('city');
$data['pincode']= $this->input->post('pincode');
$data['dob']=$this->input->post('dob');
$data['id']=NULL;
$data['accesslevel']='2';
$data['status']='1';
$data['timestamp']=NULL;
if($this->user_model->createuser($data))
{
$this->viewusers();
}
else
{
$this->createuser();
}
//$data
}
以下是使用的模型函数
public function createuser($user)
{
$query= $this->db->insert('user',$user);
if($query)
return TRUE;
return false;
}
和视图中的输入表单
<form role="form" method="post" enctype="multipart/form-data" action="<?php echo site_url('start/usersubmit') ;?>">
<input type="file" class="form-control" name="propic" id="propic" />
<input type="submit" value="submit" name="submit"/>
</form>
除上传的文件外,此处一切正常。 该文件已上传到数据库中,但大小略大于实际文件(大约多 5%),而且文件已损坏。那就是当我从数据库下载它时,文件不可读。我需要知道我的代码有什么错误。
根据聊天中的讨论,这是解决方案:
替换以下代码:
$content = fread($fp, filesize($tmpname));
$data['photo'] = addslashes($content); //it adds blackslashes after each quote(double or single)
与:
$content = fread($fp, filesize($tmpname));
$data['photo'] = $content;