如果用户没有照片codeigniter,则显示默认图像

时间:2016-02-20 18:43:11

标签: php codeigniter upload

在显示我的图像时,从数据库中获取它是正常的,但如果没有用户尚未上传照片,我想显示默认图像。我将no-avatar.jpg保存到我的/ uploads文件夹



这是我的显示用户图片。

  

视图

<img height="180px" width="180px"class="ppborder" src="<?php echo base_url().'/uploads/'.$this->session->userdata('image'); ?>"> 
  

上传

public function updatephoto($id)
    {
    if ((int)$id < 1)//$id is not an integer
    {
    redirect('Memberlogincontroller/member_view', 'refresh');
    }
    else{
    $this->load->helper(array('form','file','url'));
    $this->load->library('form_validation');
    $config_image = array(
        'upload_path'   => './uploads/',
        'allowed_types' => 'gif|jpg|png',
        'max_size'      => '1024',
        'overwrite'     => true

    );
    $this->load->library('upload', $config_image);

    if($this->form_validation->run()==false and empty($_FILES['userfile']['name'][0]))
        {
            $id = $this->session->userdata('id');
            $memberinfo = array(
                'error_image' => ''
            );
        $this->session->set_flashdata('flashError', 'Oops no photo selected', $memberinfo);         
        redirect('index.php/Memberlogincontroller/editphoto/'.$id, 'refresh');
        }       
    else
        {           
            $this->upload->do_upload();
             $data = array('upload_data' => $this->upload->data());
             $this->image_resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
             $id = $this->session->userdata('id');
             $this->db->where('id', $id);
             $query = $this->db->get('member'); 
            $data = array(
                'image' => $data['upload_data']['file_name']    
        );
        $this->db->update('member',$data,array('id'=>$id));          
        $this->session->set_userdata($data); 
        $this->session->set_flashdata('flashSuccess', 'Your photo has been updated.');
        redirect('index.php/Memberlogincontroller/getMember/'.$id, 'refresh');       
    }       
    }
    }

如何显示我的默认图片?

1 个答案:

答案 0 :(得分:1)

只需添加条件即可检查图像是否可用。如果是,则显示用户的图像,否则为默认图像。

<?php
$user_img = !empty($this->session->userdata('image')) ? $this->session->userdata('image') : 'no-avatar.jpg';
?>
<img height="180px" width="180px" class="ppborder" src="<?php echo base_url().'/uploads/'.$user_img; ?>">