2

我正在尝试将不同用户的图片路径上传到数据库中。我对 codeigniter 很陌生,我阅读了很多教程,但我仍然在挣扎。这是我的控制器,但我什至不知道如何将图像与用户会话链接。

这是我的代码:

function do_upload()

{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$config['overwrite'] = false;

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

  if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('upload_form', $error);
}else

{    
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('username', $this->input->post('username'));
$this->db->update('users', $profile);
$this->load->view('upload_success', $data);

   }

}

我的数据库有一个名为 users 的表,'profile_picture' 是路径的字段。当我上传一张图片时,我只是让我的所有用户字段都填充了相同的文件名。

4

2 回答 2

1

感谢我以这种方式解决的提示:

  function do_upload(){

    if($this->session->userdata('is_logged_in'))
    {
     $id = $this->session->userdata('id');
     $config['upload_path'] = './uploads/';
     $config['allowed_types'] = 'gif|jpg|jpeg|png';
     $config['max_size']    = '100';
     $config['max_width']  = '1024';
     $config['max_height']  = '768';
     $config['overwrite'] = false;

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

     if ( ! $this->upload->do_upload())
     {
      $error = array('error' => $this->upload->display_errors());
      $this->load->view("site_header");
      $this->load->view("site_nav");
      $this->load->view('upload_form', $error);
      }else{    

      $data = $this->upload->data();
      $file_array = $this->upload->data('file_name');
      $profile['profile_picture'] = $file_array['file_name'];
      $this->db->where('id', $id);
      $this->db->update('users', $profile);
      $this->load->view('upload_success', $data);
      }
     }else{
      $this->load->view("site_header");
      $this->load->view("site_nav");
      $this->load->view("login_view");
      $this->load->view("site_footer");
      }   
   }
于 2013-04-13T14:45:09.620 回答
0

如果您的用户有 ID,只需执行 imagePath = /upload/avatar/'.$userID.'.jpg' 或使用随机文件名并将其存储为新字段

于 2013-04-12T19:43:53.170 回答