繁体   English   中英

如何在codeigniter中上传多个图像和克里特拇指,以及如何将它们的名称与其他文件存储在数据库中

[英]How to upload multiple images and crete thumb in codeigniter and also store their name in database with other fileds

我想上传3张图像并创建它们的缩略图,然后将其名称存储在databse中。 不知道该怎么办。 注意我已经阅读了很多论坛和问题,但没有一个帮助我完成上传和存储到数据库中。这是我对1张图片所做的工作,如果我可以对多张图片进行相同的修改而无需做任何修改,这将对我有帮助

View

 <p> <label>Image 1</label> <input type="file" name="userfile" value=""/> // I want same kind of anothe two uploads say image2,image3 </p> 

Controller

 function insert() { $data = array('title'=>'Add New Image', 'link_add'=>site_url('manage/img_gallaryslider/add'), 'edit_link'=>site_url('manage/img_gallaryslider/edit'), 'action'=>site_url('manage/img_gallaryslider/insert'), 'link_back'=>site_url('manage/img_gallaryslider'), 'tbl'=>'imgslider'); $this->_set_fields(); $this->_set_rules(); if ($this->form_validation->run() == TRUE) { $config['upload_path'] = './images/imagegallaryslider'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); if($_FILES['userfile']['name']=='') { $data['upload_error']='<strong>Error: Please, select image to upload</strong>'; } else { $data['upload_error']='<strong>Error:Invalid file format or size</strong>'; } $this->load->view('manage/includes/header', $data); $this->load->view('manage/img_gallaryslideredit', $data); $this->load->view('manage/includes/footer'); } else { $data = array('upload_data' => $this->upload->data()); $filename= $this->upload->data(); $file_name=$this->upload->file_name; $this->create_thumb($file_name); // save data $this->ip_date = $this->admin_model->get_date_ip(); $value_array = array('Name' => $this->input->post('name'), 'Image'=>$this->upload->file_name, 'CreatedBy' => $this->session->userdata('adminid'), 'CreatedDate'=>$this->ip_date->cur_date, 'CreatedIp'=>$this->ip_date->ip); $id = $this->admin_model->save('imggallaryslider',$value_array); $this->session->set_flashdata('notification',$this->lang->line('gen_succ_added')); redirect(site_url('manage/img_gallaryslider/index')); die(); } } else { $this->load->view('manage/includes/header', $data); $this->load->view('manage/img_gallaryslideredit', $data); $this->load->view('manage/includes/footer'); } } function create_thumb($file_name) { $config['image_library'] = 'gd2'; $config['source_image'] = './images/imagegallaryslider/'.$file_name; $config['create_thumb'] = FALSE; $config['maintain_ratio'] = TRUE; $config['width'] = 70; $config['height'] = 50; $config['new_image'] = './images/imagegallaryslider/thumb/'.$file_name; $this->load->library('image_lib', $config); $this->image_lib->resize(); if(!$this->image_lib->resize()) { echo $this->image_lib->display_errors(); } } model 

function insert() { $data = array('title'=>'Add New Image', 'link_add'=>site_url('manage/img_gallaryslider/add'), 'edit_link'=>site_url('manage/img_gallaryslider/edit'), 'action'=>site_url('manage/img_gallaryslider/insert'), 'link_back'=>site_url('manage/img_gallaryslider'), 'tbl'=>'imgslider'); $this->_set_fields(); $this->_set_rules(); if ($this->form_validation->run() == TRUE) { $config['upload_path'] = './images/imagegallaryslider'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '1000'; $config['max_width'] = '1024'; $config['max_height'] = '768'; $this->load->library('upload', $config); if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); if($_FILES['userfile']['name']=='') { $data['upload_error']='<strong>Error: Please, select image to upload</strong>'; } else { $data['upload_error']='<strong>Error:Invalid file format or size</strong>'; } $this->load->view('manage/includes/header', $data); $this->load->view('manage/img_gallaryslideredit', $data); $this->load->view('manage/includes/footer'); } else { $data = array('upload_data' => $this->upload->data()); $filename= $this->upload->data(); $file_name=$this->upload->file_name; $this->create_thumb($file_name); // save data $this->ip_date = $this->admin_model->get_date_ip(); $value_array = array('Name' => $this->input->post('name'), 'Image'=>$this->upload->file_name, 'CreatedBy' => $this->session->userdata('adminid'), 'CreatedDate'=>$this->ip_date->cur_date, 'CreatedIp'=>$this->ip_date->ip); $id = $this->admin_model->save('imggallaryslider',$value_array); $this->session->set_flashdata('notification',$this->lang->line('gen_succ_added')); redirect(site_url('manage/img_gallaryslider/index')); die(); } } else { $this->load->view('manage/includes/header', $data); $this->load->view('manage/img_gallaryslideredit', $data); $this->load->view('manage/includes/footer'); } } function create_thumb($file_name) { $config['image_library'] = 'gd2'; $config['source_image'] = './images/imagegallaryslider/'.$file_name; $config['create_thumb'] = FALSE; $config['maintain_ratio'] = TRUE; $config['width'] = 70; $config['height'] = 50; $config['new_image'] = './images/imagegallaryslider/thumb/'.$file_name; $this->load->library('image_lib', $config); $this->image_lib->resize(); if(!$this->image_lib->resize()) { echo $this->image_lib->display_errors(); } } model

 function save($table,$value) { $this->db->insert($table, $value); return $this->db->insert_id(); } </code> 

声明为public function do_upload($field = 'userfile')的方法do_upload() ,因此您可以在代码中使用它,例如:

private function uploads() {
   $config['upload_path'] = './images/imagegallaryslider';
   $config['allowed_types'] = 'gif|jpg|png';
   $config['max_size']  = '1000';
   $config['max_width']  = '1024';
   $config['max_height']  = '768';
   $this->load->library('upload', $config);
   //<--HERE the several images handler
   foreach (array('userfile','img1','img2' as $field) { 
    if ( ! $this->upload->do_upload($field))
    {
        $error = array('error' => $this->upload->display_errors());
        if($_FILES[$field]['name']=='')
    {
     $data['upload_error']='<strong>Error: Please, select image to upload</strong>';
    }
   else
    {
        $data['upload_error']='<strong>Error:Invalid file format or size</strong>';
    }
    return $data;
   }
   $data = array('upload_data' => $this->upload->data());
   $filename= $this->upload->data();
   $file_name=$this->upload->file_name;
   $this->create_thumb($file_name);
     // save data
   $this->ip_date = $this->admin_model->get_date_ip();
   $value_array = array('Name' => $this->input->post('name'),
                        'Image'=>$this->upload->file_name,
                     'CreatedBy' => $this->session->userdata('adminid'),
                     'CreatedDate'=>$this->ip_date->cur_date,
                     'CreatedIp'=>$this->ip_date->ip);
   $id = $this->admin_model->save('imggallaryslider',$value_array);
 }
 $this->session->set_flashdata('notification',$this->lang->line('gen_succ_added'));
 redirect(site_url('manage/img_gallaryslider/index'));
 die();
}

鉴于它应该看起来像:

<p>
  <label>Image 1</label>
  <input type="file" name="userfile"  value=""/>
  <input type="file" name="img1"  value=""/>
  <input type="file" name="img2"  value=""/> 
</p>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM