file_get_contents()期望参数1是有效路径

时间:2015-06-01 08:50:12

标签: image codeigniter model-view-controller controller file-get-contents

我的图片正在成功上传到图片/新闻。我想现在将该图像保存在数据库中。但它一直给我上述错误。我的模型查询是正确的,因为我使用它来插入其他数据,它的工作原理。

用户点击提交后,如何将图像保存到数据库中?

我的控制器:

 function news()
{

 $config = array(
'upload_path' => "./images/news",
'allowed_types' => "gif|jpg|png|jpeg|JPG",
'overwrite' =>False,
'max_size' => "2048000", 
'max_height' => "768",
'max_width' => "1024"
);
    $this->load->library('upload', $config);

    $this->load->helper(array('form', 'url'));

    if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
 $this->load->view('manage_news',$data);
}
else
 {
 $message2 = "Please choose another file type. gif,jpg,png,jpeg,pdf ";
         echo "<script type='text/javascript'>alert('$message2');   </script>";                 
          redirect('resetPasswordController/add_news', 'refresh');
       }

        $this->db->trans_start();

        $data = array('upload_data' => $this->upload->data());

        $data = array(          
            'image' => file_get_contents( $data )
            );

         $this->load->model('users_model'); 
        $this->users_model->insert($data);    

        $this->db->trans_complete();
        $this->db->trans_complete();

        redirect('resetPasswordController/manage_news', 'refresh');
  }

我的观点:

 <?php echo form_open_multipart('resetPasswordController/news');?>
 <label>Image</label>
<input name = "userfile" type="file" />

我的模特:

 function insert($data){
        $this->db->insert('news',$data);
        return $this->db->insert_id();
}

1 个答案:

答案 0 :(得分:0)

$ data包含数组值,您必须使用upload_data

代表:

$ data = array(&#39; upload_data&#39; =&gt; $ this-&gt; upload-&gt; data());

$data = array(          
    'image' => file_get_contents( $data )
    );

将此更改为

$data = array('upload_data' => $this->upload->data());

        $data = array(          
            'image' => file_get_contents( $data['upload_data'] )
            );

您可以直接使用上传数据功能

            $data = array(          
                'image' => file_get_contents(  $this->upload->data())
                );