gpt4 book ai didi

php - 将文件以 blob 格式插入数据库表

转载 作者:行者123 更新时间:2023-11-29 23:16:12 25 4
gpt4 key购买 nike

我正在使用 codeigniter 框架,并且我正在尝试将 blob 内容(本例中为图像)上传到数据库表中

以下是我的 Controller 代码

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

以及 View 中的输入表单

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

关于php - 将文件以 blob 格式插入数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27782651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com