我将配置文件图像加载到名为profiles的数据库中,但我如何在屏幕上实际显示配置文件?

时间:2012-10-25 22:32:04

标签: php codeigniter

我的个人资料模型文件:

function putProfileImage($user, $image)
{


    $record = array('user' => $user, 'profileimage' => $image);
    if ($this->exists($user))
    {
        $this->db->where('user', $user)->update('profiles', $record);
    }
    else
    {
        $this->db->where('user', $user)->insert('profiles', $record);
    }
}

function getProfileImage($user)
{

    $config = array(
            'allowed_types' => 'jpg|jpeg|gif|png',
            'height' => 200,
            'length' => 150
    );


    $this->db->select('*')->from('profiles')->where('user', $user);
    $query = $this->db->get();
    $row = $query->row();
    return $row->profileimage; //Trying to get property of non-object error


}

它的getProfileimage,类似于do_upload函数,其中我遇到的麻烦最多

3 个答案:

答案 0 :(得分:1)

看看这些网站

然后按照下面的伪代码(假设profileimage在数据库中存储为BLOB字段)

模型文件:

function putProfileImage($user, $image)
{
    // you may need to store the image type / mine type
    $record = array('user' => $user, 'profileimage' => $image);
    if ($this->exists($user))
    {
        $this->db->update('profiles', $record)->where('user', $user);
    }
    else
    {
        $this->db->insert('profiles', $record);
    }
}

function getProfileImage($user)
{
    $this->db->select('*')->from('profiles')->where('user', $user);
    $query = $this->db->get();
    if ($query->num_rows() > 0){
        $row = $query->row();
        return $row->profileimage; //Trying to get property of non-object error
    }

    return Null;
}

Controller - profile.php

function Upload()
{
    // see - Creating the Upload Form
    // @ - http://codeigniter.com/user_guide/libraries/file_uploading.html
}

function doUpload ()
{
    //see do_upload() and save with module above
    // @ - http://codeigniter.com/user_guide/libraries/file_uploading.html
}

function displayImage()
    {
        //Retrieve image id from the URI
        $imageid = $this->uri->segment(3);
        //Initialize the images model
        $this->load->model("image_model");
        //Call the model's getImage function passing the image id
        $image = $this->image_model->getProfileImage($imageid);
        if (!is_null($image)) {
            //need to know the mine type 
           // header('Content-Type: image/png');

            header ('Content-Type: image/jpg');
            imagejpeg(imagecreatefromstring($image), null,100);
        }
        else{
         // load a default profile image
        }
    }

    function viewProfile(){
        //load profile detail view for users and display image
        // 18 = change to image id
        echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';

    }

答案 1 :(得分:0)

* 尝试获取非对象* 错误的属性意味着查询最有可能没有结果,即配置文件中没有行表格 user = $ user

为了帮助您调试,您可以在 $ query = $ this-&gt; db-&gt; get();

之后立即使用此命令
echo $this->db->last_query(); die();

* $ this-&gt; db-&gt; last_query(); *将获取上次查询运行的sql, die()将终止该程序。

答案 2 :(得分:0)

我设法通过保存到正确的图像位置文件夹

来解决此问题