如何将路径图像用户存储到数据库:Codeigniter [英] How to store path image users into a database: Codeigniter

查看:175
本文介绍了如何将路径图像用户存储到数据库:Codeigniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将不同用户的图片的路径上传到数据库。
我对codeigniter非常新,我阅读了很多教程,但我很挣扎。
这是我的控制器,但我甚至不知道如何链接图像与用户会话。

I am trying to upload the paths of the pictures for different users into at database. I am quite new on codeigniter and I read a lot of tutorials, but I am sill struggling. This is my controller but I don't even know how to link the image with the user session.

这是我的代码:

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'是该路径的字段。当我上传图片时,我只是将所有使用者栏位填入相同的档案名称。

My database has a table called users and 'profile_picture' is the field for the path. When I upload an image, I just get all my users fields filled with the same file name.

推荐答案

我以这种方式解决:

  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");
      }   
   }

这篇关于如何将路径图像用户存储到数据库:Codeigniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆