如何在codeigniter中显示上传的照片

时间:2014-01-02 22:51:10

标签: php codeigniter image-uploading

我正在codeigniter中构建用户个人资料。我已经设置了一个系统,允许用户使用其用户ID创建一个文件夹,然后将照片上传到该文件夹​​。我已成功将文件上传到文件夹,但我不确定如何在我的视图中显示与用户相关的照片。

我会假设我将它附加到我的$user[''],只是不确定如何做到这一点。提前致谢。

public function upload()
    {
        $this->load->library('session');
        $this->load->helper('url');
        $session_id = $this->session->userdata('id');
        $this->load->model('account_model');
        $user = $this->account_model->user();
        $data['user'] = $user;
        echo $user['id'];
        $user_folder = './uploads/' . $this->session->userdata('id');
        if(!is_dir($user_folder)){

            mkdir($user_folder, 0777);
        }
        $config['image_library'] = 'gd2';
        $config['source_image'] = $user_folder;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 50;
        $config['height']   = 50;

        $this->load->library('image_lib', $config); 

        $this->image_lib->resize();
        $config['upload_path'] = $user_folder;
        $config['allowed_types'] = 'gif|jpg|png';
        //$config['max_size']   = '165';
        $config['max_width']  = '165';
        $config['max_height']  = '165';

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


        if ( ! $this->upload->do_upload())
        {

            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = '/account/upload';
            $this->load->view('includes/templates/main_page_template', $data);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $data['main_content'] = '/account/success';
            $this->load->view('includes/templates/main_page_template', $data);
        }
    }
    public function img(array $user=array()) {
        if(!isset($user['relation'])) {
            return '';
        }
        $path = '';
        if($user['relation'] != 'Choose One') {
            $path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
        }
        return $path;
    }
}

3 个答案:

答案 0 :(得分:1)

控制器:

public function view_image($user_id)
{
     $this->load->model('account_model');

     $data['image'] = $this->account_model('model_that_will_fetch_relevant_image');

     $this->load->view('where_image_is',$data);

}

模型:

public function get_image($user_id)
{ 

 $this->db->select('stuff'): \\ Select image filename

 $this ->db->where('table_that_has_user_id', $user_id ); 

 $query =  $this->db->get($this->_table_name . ' as aliase'); \\ where filename is

 return $query->result();
 }

视图:

<img src = "<?=site_url('path/on/server/' . $image['image_filename']); ?>">

这假设您将图像文件名存储在mysql数据库中。

答案 1 :(得分:0)

class Users_model extends CI_Model {

   var $table = 'tbl_admin';

   function __construct(){
       parent::__construct();
       $this->load->database();
   }

   public function processLogin($username,$password){ 
       $this->db->select('username,password');
       $where = $array = array('username' =>$username,'password'=>$password);
       $this->db->where($where);
       $this->db->from($this->table);
       $query = $this->db->get();
       return $query;
   }
}

答案 2 :(得分:0)

请使用此代码,希望对您有所帮助。

使用 profile_image 变量可以显示图像。

<?php 

public function upload()
    {
        $this->load->library('session');
        $this->load->helper('url');
        $session_id = $this->session->userdata('id');
        $this->load->model('account_model');
        $user = $this->account_model->user();
        $data['user'] = $user;
        echo $user['id'];
        $user_folder = './uploads/' . $this->session->userdata('id');
        if(!is_dir($user_folder)){

            mkdir($user_folder, 0777);
        }
        $config['image_library'] = 'gd2';
        $config['source_image'] = $user_folder;
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 50;
        $config['height']   = 50;

        $this->load->library('image_lib', $config); 

        $this->image_lib->resize();
        $config['upload_path'] = $user_folder;
        $config['allowed_types'] = 'gif|jpg|png';
        //$config['max_size']   = '165';
        $config['max_width']  = '165';
        $config['max_height']  = '165';

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


        if ( ! $this->upload->do_upload())
        {

            $error = array('error' => $this->upload->display_errors());
            $data['main_content'] = '/account/upload';
            $this->load->view('includes/templates/main_page_template', $data);
        }
        else
        {
            $image = array('upload_data' => $this->upload->data());
            $data['profile_image'] = $user_folder.$image['upload_data']['file_name'];
            $data['main_content']  = '/account/success';
            $this->load->view('includes/templates/main_page_template', $data);
        }
    }
    public function img(array $user=array()) {
        if(!isset($user['relation'])) {
            return '';
        }
        $path = '';
        if($user['relation'] != 'Choose One') {
            $path = '/styles/images/Sharpie' . $user['relation'] . '.gif';
        }
        return $path;
    }
}