【问题标题】:Inserting a file into database table in blob format以blob格式将文件插入数据库表
【发布时间】:2015-01-05 15:29:14
【问题描述】:

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

【问题讨论】:

  • 您的问题:$data['photo'] = addslashes($content); 当 CI insert() 方法已经为您转义了您的数据时,您为什么需要这样做?
  • 另外:stackoverflow.com/questions/7845233/… 建议使用rb 作为fopen 模式。
  • @sjagr ,没有这个就绝对没有文件上传,NULL 值显示在数据库中。看到这个php-mysql-tutorial.com/wikis/mysql-tutorials/…
  • 那没有意义,能不能在fread行后面输出$content的值?
  • @Ofir Baruch,请原谅我,它没有给出 null 但事情保持不变,我仍然得到一个损坏的文件

标签: php mysql codeigniter


【解决方案1】:

根据聊天中的讨论,这是解决方案:

替换以下代码:

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

【讨论】:

    猜你喜欢
    • 2011-05-16
    • 1970-01-01
    • 2016-11-16
    • 2012-05-15
    • 2014-12-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多