php - codeigniter upload file with another fields -
i have in application view :
<div class="form-group"> <label class="col-md-4 control-label" for="content_textarea">content</label> <div class="col-md-4"> <textarea class="form-control" id="content_textarea" name="ad_news_content"></textarea> </div> </div> <!-- select basic --> <div class="form-group"> <label class="col-md-4 control-label" for="news_category">category</label> <div class="col-md-4"> <select id="news_category" name="ad_news_category" class="form-control"> <?php foreach ($news_category_data $ncat ) {?> <option value="<?php echo $ncat->nc_id ?>"><?php echo $ncat->nc_id ," - ", $ncat->news_category_name ?> </option> <?php } ?> </select> </div> </div> <div class="form-group"> <label class="col-md-4 control-label" for="news_create_date">date</label> <div class=" col-md-5"> <input type="text" placeholder="click show datepicker" id="datepic"> </div> </div> <!-- main image upload --> </div> <div class="form-group"> <label class="col-md-4 control-label" for="news_upload_img">main image</label> <div class=" col-md-5"> <input type="file" name="userfile" size="20" /> </div> </div> <!-- end of main image upload --> <!-- button (double) --> <div class="form-group"> <label class="col-md-4 control-label" for="edit_btn"></label> <div class="col-md-8"> <button id="edit_btn" name="add_btn" class="btn btn-primary" formaction="<?php echo base_url() ."admin/news/insertnews"?>">update</button>
and in controller have function insert data base , upload image
public function insertnews() { $config = array( 'upload_path' => './uploads/up_news', 'allowed_types' => 'gif|jpg|png', 'max_size' => '100', 'max_width' => '1024', 'max_height' => '768', 'encrypt_name' => true, ); $this->load->library('upload', $config); $this->upload->initialize($config); $upload_data = $this->upload->data(); $data_ary = array( 'title' => $upload_data['client_name'], 'file' => $upload_data['file_name'], 'width' => $upload_data['image_width'], 'height' => $upload_data['image_height'], 'type' => $upload_data['image_type'], 'size' => $upload_data['file_size'], 'path' => $upload_data['full_path'], 'date' => time(), ); $data = array('upload_data' => $upload_data); $this->load->model('newsmodel'); $ad_ne_data = array( 'titel' => $this->input->post('ad_news_title') , 'content' => $this->input->post('ad_news_content') , 'news_category_id' => $this->input->post('ad_news_category') , 'img_url' => $data_ary['path']."".$data_ary['file'], 'created_at' => date("y-m-d") ); $this->newsmodel->addnews($ad_ne_data); }
the code run ok image not uploaded , in database insert upload path without image name not full path can correct wrong or give me example upload image other fields every image have spectate folder news in uploaded_news, posts in uploaded_post
when need upload image need use do_upload() recommend using form_helper form_open_multipart
$config = array( 'upload_path' => './uploads/up_news', 'allowed_types' => 'gif|jpg|png', 'max_size' => '100', 'max_width' => '1024', 'max_height' => '768', 'encrypt_name' => true, ); $this->load->library('upload', $config); if ($this->upload->do_upload('userfile')) { $upload_data = $this->upload->data(); $data_ary = array( 'title' => $upload_data['client_name'], 'file' => $upload_data['file_name'], 'width' => $upload_data['image_width'], 'height' => $upload_data['image_height'], 'type' => $upload_data['image_type'], 'size' => $upload_data['file_size'], 'path' => $upload_data['full_path'], 'date' => time(), ); $data = array('upload_data' => $upload_data); $this->load->model('newsmodel'); $ad_ne_data = array( 'titel' => $this->input->post('ad_news_title') , 'content' => $this->input->post('ad_news_content') , 'news_category_id' => $this->input->post('ad_news_category') , 'img_url' => $data_ary['path']."".$data_ary['file'], 'created_at' => date("y-m-d") ); $this->newsmodel->addnews($ad_ne_data); } else { // error }
second idea using callback
filename: example.php
class example extends ci_controller { public function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->helper('form'); } public function index() { $data['title'] = 'ad'; $this->form_validation->set_rules('ad_news_title', 'ad title', 'required'); $this->form_validation->set_rules('ad_news_content', 'content', 'callback_insertnews'); if ($this->form_validation->run() == false) { $this->load->view('your_view', $data); } else { redirect('success_controller'); } } public function insertnews() { $config = array( 'upload_path' => './uploads/up_news', 'allowed_types' => 'gif|jpg|png', 'max_size' => '100', 'max_width' => '1024', 'max_height' => '768', 'encrypt_name' => true, ); $this->load->library('upload', $config); if ($this->upload->do_upload('userfile')) { $upload_data = $this->upload->data(); $data_ary = array( 'title' => $upload_data['client_name'], 'file' => $upload_data['file_name'], 'width' => $upload_data['image_width'], 'height' => $upload_data['image_height'], 'type' => $upload_data['image_type'], 'size' => $upload_data['file_size'], 'path' => $upload_data['full_path'], 'date' => time(), ); $data = array('upload_data' => $data_ary); $this->load->model('newsmodel'); $ad_ne_data = array( 'titel' => $this->input->post('ad_news_title') , 'content' => $this->input->post('ad_news_content') , 'news_category_id' => $this->input->post('ad_news_category') , 'img_url' => $data_ary['path']."".$data_ary['file'], 'created_at' => date("y-m-d") ); $this->newsmodel->addnews($ad_ne_data); } else { // error $this->form_validation_set_message('insertnews', $this->upload->display_errors()); return false; } } }
view form open
<?php echo form_open_multipart('example/insertnews');?> // rest of form content goes here <?php echo form_close();?>
make sure have set base url in ci3
Comments
Post a Comment