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

时间:2015-01-05 15:29:15

标签: php mysql codeigniter

我正在使用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%),并且文件也会损坏。那是当我从数据库下载它时,该文件是不可读的。我需要知道我的代码中的错误。

1 个答案:

答案 0 :(得分:0)

在聊天讨论之后,这就是解决方案:

替换以下代码:

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