Php 如何将路径图像用户存储到数据库:Codeigniter
我正在尝试将不同用户的图片路径上传到at数据库中。 我对codeigniter是一个新手,我读了很多教程,但我很挣扎。 这是我的控制器,但我甚至不知道如何将映像与用户会话链接 这是我的密码:Php 如何将路径图像用户存储到数据库:Codeigniter,php,codeigniter,file-upload,image-uploading,Php,Codeigniter,File Upload,Image Uploading,我正在尝试将不同用户的图片路径上传到at数据库中。 我对codeigniter是一个新手,我读了很多教程,但我很挣扎。 这是我的控制器,但我甚至不知道如何将映像与用户会话链接 这是我的密码: function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|jpeg|png'; $config['max_size'] = '100'; $config['max_wi
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”是路径的字段。当我上传一张图片时,我只会让我所有的用户字段都填写相同的文件名。如果您的使用有ID,只需执行imagePath=/upload/avatar/'.$userID..jpg'或使用随机文件名并将其存储为新字段。感谢我以这种方式解决的提示:
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");
}
}
用户会话意味着您有一个已登录的用户,这意味着您有他/她的唯一id,因此在where语句中使用它,而不是用户名&从会话中而不是从post变量中获取它。