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 Null;


}

   }

这是我的控制器:

class HomeProfile extends CI_Controller 
 {


function HomeProfile()
  {
   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');
$this->profileimages->putProfileImage($username, $this->input->post("profileimage"));
//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();


    $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->username = $this->session->userdata('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['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');
   }

  }

如果您查看模型中的 putprofileimages 函数,我想我会在此处的某处放置一个 appends 命令。我希望图像的文件名出现在 profileimage 字段的数据库中。我也希望当我刷新时个人资料图像会留在屏幕上

<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>  
   <div id="secondary">
  <p>
      <?=$profileText;?>
    </p>
    <p>
      <?=form_open('homeprofile/changetext'); ?>
      <?php $msgbox = array(
            'name' => 'profiletext',
            'rows' => '8',
            'cols' => '30',
            );?>
     <?=form_textarea($msgbox);?>       
     </p>
     <p>
      <?=form_submit('submit', 'Change'); ?>
      <?=form_close(); ?>
    </p>
</div>
4

2 回答 2

1

您可以像 char 字段一样简单地将图像名称保存在数据库中。就在您的do_upload功能之后,您可以使用检索图像名称$this->upload->file_name

然后你可以很容易地将它保存在数据库中,而不是整个文件。

现在,在您的视图页面中,您可以通过 html 设置图像路径,然后仅附加您从 db 查询中获得的文件名。

于 2012-11-24T08:38:39.657 回答
0

一个简短的说明 - 你在使用 codeigniter 2 吗?你应该这样设置你的构造函数:

 class HomeProfile extends CI_Controller 
 {

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

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

 } // end construct

模型也一样!好的,在你的模型中,也许我没有得到这个,但你有

 if ($this->exists($username))

但你的方法说

 function putProfileImage($username, $img)

所以如果用户名可能是空白的,你只需要设置一个默认值

function putProfileImage($username = 0, $img)

那么也许你可以做

 if ($username === 0)

但我个人不会将插入和更新放在同一个方法中,将它们分开,调试起来会容易得多。还有——你是否在自动加载中加载了会话库???

于 2012-11-25T01:40:52.297 回答