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 に答える