php - upload image in codeigniter -
i'm trying upload image in codeigniter, got problem in , $this->upload->do_upload('imgname'). if condition not execute, elese execute , show error message 'you did not selected file upload'. while remove form upload in code execute perfectly....
controller:
public function add_news() { $post = $this->input->post(); unset($post['submit']); $this->load->model('adminmodel','addnews'); if(!is_dir('uploads')) { mkdir(base_url().'uploads',0777,true); } if(!is_dir('uploads/news')) { mkdir('uploads/news',0777,true); } $config = [ 'upload_path'=>'uploads/news', 'allowed_types'=>'png|jpg|jpeg|gif', 'encrypt_name'=>'0777', ]; $this->load->library('upload', $config); if($this->upload->do_upload('newsimg')) { $data = $this->upload->data(); $file_path = base_url().'uploads/news/'.$data["raw_name"].$data["file_ext"]; $post['newsimg'] = $file_path; //$this->revedit->reg_info($id,$post); $post['posted'] = date('d-m-y'); $this->addnews->store_news($post); $this->session->set_flashdata('newsalert','yeh! news added successfully.'); redirect('admin/news'); } else { $newsdata = $this->addnews->news_list(); $upload_error = $this->upload->display_errors(); $this->load->view('admin/news',compact('upload_error','newsdata')); }}
view:
<?=form_open('admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminid')])?> <div class="row"> <div class="col-sm-12"> <div class="form-group"> <label>news heading</label> <?=form_input(['name'=>'title','class'=>'form-control','placeholder'=>'enter news title','value'=>set_value('title')])?> </div> <div class="form-group"> <label>news details</label> <?=form_textarea(['name'=>'body','class'=>'form-control','placeholder'=>'enter news body','value'=>set_value('body')])?> </div> <div class="form-group col-sm-3"> <div class="fileupload btn btn-primary" data-toggle="tooltip" data-placement="top" title="pdf file only"> <span><i class="fa fa-cloud-upload" aria-hidden="true"></i> select file <span id="percentage" class="percentage text-center"></span></span> <?=form_upload(['name'=>'newsimg','class'=>'upload','id'=>'file1','onchange'=>'loadpdf(event)'])?> </div> </div> <div class="col-sm-12"> <div id="myprogress" class="progress"> <div id="mybar" class="bar progress-bar" role='progressbar'> </div> </div> </div> </div> </div> </div> <div class="modal-footer"> <?=form_reset('reset','reset',['class'=>'btn btn-md btn-primary'])?> <?=form_submit('submit','post',['class'=>'btn btn-md btn-success'])?> </div> <?=form_close()?>
model:
public function store_news($array) { return $this->db->insert('tbl_news', $array); }
you not using enctype='multipart/form-data'
try this
<?=form_open_multipart('admin/add_news',['class'=>'form'],['aid'=>$this->session->userdata('adminid')])?>
Comments
Post a Comment