0

我需要将图像保存到会话中:

到目前为止,这是我的代码:

模型:

  function ProfileImages()
   {
    parent::__construct();


    }

  function exists($username)
    {
     $this->db->select('*')->from("profileimages")->where('user', $username);
     $query = $this->db->get();

     if ($query->num_rows() > 0)
     {

        return true;
        /*
         echo "user $user exists!";
        $row = $query->row();
        echo " and his profileimage is $row->profileimage";
        */
       }

    else

        {

        return false;
        //echo "no such user as $user!";
         }

    }


function putProfileImage($username, $img)
{


    $record = array('user' => $username, 'profileimage' => $img);
    if ($this->exists($username))
    {
        $this->db->where('user', $username)->update('profileimages', $record);


    }
    else
    {
        $this->db->where('user', $username)->insert('profileimages', $record);

    }

}

function getProfileImage($username)
{

    $this->db->select('*')->from('profileimages')->where('user', $username);

    $query = $this->db->get();
      if ($query->num_rows() > 0){
    $row = $query->row();
    return $row->profileimage;
     }

     return "";
    }
}

控制器:

function __construct()
{
// Call the parent construct
   parent::__construct();

   $this->load->model("profiles");
   $this->load->model("profileimages");
   $this->load->helper(array('form', 'url'));      

} 


function upload()
{
    $config = array(

        'allowed_types' =>'gif|jpg|jpeg|png',
        'upload_path' =>'./web-project-jb/assets/puploads/',
         'max_size' => 10000,
         'max_width' => 1024,
        'max_height' => 768);


    $this->load->library('upload', $config);
    $img = $this->session->userdata('img');
    $username = $this->session->userdata('username');
//fail show upload form
if (! $this->upload->do_upload())
{

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

    $username = $this->session->userdata('username');


    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);

    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('homeprofile/homeprofileview', $error, $viewData, array('error' => ' ' ));
    $this->load->view('shared/footer');

    //redirect('homeprofile/index');

   }

  else
   {
    //successful upload so save to database


    $file_data = $this->upload->data();

    $img = $file_data['file_name'];

    $data['img'] = '/web-project-jb/assets/puploads/'.$file_data['file_name'];
    // you may want to delete the image from the server after saving it to db
    // check to make sure $data['full_path'] is a valid path
    // get upload_sucess.php from link above
    //$image = chunk_split( base64_encode( file_get_contents( $data['file_name'] ) ) );

    $this->img = $this->session->userdata('img');


    $this->username = $this->session->userdata('username');

    $this->profileimages->putProfileImage($username, $img);

    $data['username'] = $username;
    $data['profileimages'] = $this->profileimages->getProfileImage($username);


    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);

    $username = $this->session->userdata('username');

    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    $this->load->view('homeprofile/homeprofileview', $data, $viewData);
    $this->load->view('shared/footer');


    //redirect('homeprofile/index');
    }

 }



   function index()
  {

    $username = $this->session->userdata('username');

    $data['username'] = $username;
    $data['profileimages'] = $this->profileimages->getProfileImage($username);

    $viewData['username'] = $username;
    $viewData['profileText'] = $this->profiles->getProfileText($username);

    $this->load->view('shared/header');
    $this->load->view('homeprofile/homeprofiletitle', $viewData);
    $this->load->view('shared/nav');
    //$this->load->view('homeprofile/upload_form', $data);
    $this->load->view('homeprofile/homeprofileview', $data, $viewData, array('error' => ' ' ) );
    $this->load->view('shared/footer');
  }

 }

看法:

<h3><?="Profile Image"?></h3>
 <img src="<?php if (isset($img)) echo base_url($img); ?>" 
 width='300' height='300'/>
  <?=form_open_multipart('homeprofile/upload');?>
    <input type="file" name="userfile" value=""/>
    <?=form_submit('submit', 'upload')?>
    <?=form_close();?> 
    <?php if (isset($error)) echo $error;?>
  </div> 
</div> 

我在想我实际上是否需要将图像作为链接保存到我的数据库中,而不是文件名?get profileimage 函数旨在将图像扔回屏幕,使其停留在那里。再次感谢您的所有帮助。

4

1 回答 1

0

这条线是问题所在:

$this->profileimages->putProfileImage($username, $img);

您正在保存image,但不是路径。因此,正如您所说,您需要保存实际路径:

$img = '/web-project-jb/assets/puploads/'.$file_data['file_name'];
...etc
$this->profileimages->putProfileImage($username, $img);

希望这可以帮助您解决您遇到的问题。

编辑

关于session尝试做类似的事情:

$this->session->set_userdata($img); //note I've changed the name above as well.

查看 codeigniter 会话类指南以获取更多参考。

于 2012-11-27T23:42:14.357 回答