如何显示数据库中的图像

时间:2013-04-15 14:30:02

标签: php codeigniter

我在显示存储在服务器中的图像(/ uploads)时遇到问题,并且该字段的名称存储在数据库中并与用户ID链接。

我的控制员:

function do_upload()
{
if($this->session->userdata('is_logged_in'))
{
$id = $this->session->userdata('id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('upload_form', $error);
}else
{    
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('id', $id);
$this->db->update('users',  $profile);
$data['img']= base_url().'/uploads/'.$data['file_name'];
$this->load->view('upload_success', $data);
}


public function members(){
if($this->session->userdata('is_logged_in')){
   $this->load->model('loginmodel');
   $data = $this->loginmodel->getImage();
   print('/uploads/'.$data);
   $img = base_url().'/uploads/'.$data;
   $this->load->view("site_header");
   $this->load->view("site_nav");
   $this->load->view("members");
   $this->load->view("site_footer");

}else{.....

模型

function getImage()
{
$id = $this->session->userdata('id');
$this->db->select("*");
$this->db->from('users');
$this->db->where('id', $id);
$query = $this->db->get();
if($query->num_rows()==0)
echo("Picture not found!");
else{
$data = $query->row_array();
return $data['profile_picture'];
 }
}

视图:

<img src="<?php echo $img; ?>" border="0" width="10" height="10">

我收到此错误: 遇到PHP错误 严重性:注意 消息:未定义的变量:img 文件名:views / members.php 行号:19 “border =”0“width =”10“height =”10“&gt;

2 个答案:

答案 0 :(得分:0)

将数据传递给视图。

$data = array('img' => $img);
$this->load->view("members", $data);

答案 1 :(得分:0)

您的查询可能无法返回数据,因为可能没有匹配项 尝试在视图中使用php的isset函数