php - codeigniter upload file $_FILES returns empty array array(0) { } -
i have function upload image inserts image name database fine. created new function called edit()
exact same code allows user edit form , upload image. image name update existing image name in database.
however, every time user edits form , chooses new image, instead of saving new image name, wipes the previous name database , display nothing.
my controller:
public function edit(){ $this->form_validation->set_rules('title', 'title', 'required|xss_clean|trim|is_unique[news.title]'); $this->form_validation->set_rules('description', 'description', 'required|xss_clean|trim'); $this->form_validation->set_rules('notes', 'notes', 'required|xss_clean|trim'); if ($this->form_validation->run() == false) { $data['error'] = ''; $data['page_title']="edit news"; $this->load->view("edit_news_form",$data); } else { $new = array( 'upload_path' => "./images/news", 'allowed_types' => "gif|jpg|png|jpeg|jpg", 'overwrite' =>false, 'max_height' => "768", 'max_width' => "1024" ); $this->load->library('upload', $new); $this->load->helper(array('form', 'url')); if(isset($_files)) { if($this->upload->do_upload()) { $data = array('upload_data' => $this->upload>data()); $this->load->view('manage_news',$data); } else { if(empty($_files['userfile']['name'])){ $imagename ='';} else { $message2 = "please choose file type. gif,jpg,png,jpeg "; echo "<script type='text/javascript'>alert('$message2');</script>"; redirect('resetpasswordcontroller/edit_news_form', 'refresh'); } } } $this->db->trans_start(); $imagename = $this->upload->data(); $data = array( 'image'=>$imagename['file_name'], ); $this->users_model->update($id,$data); $this->db->trans_complete(); $message2 = "the selected news has been edited"; echo "<script type='text/javascript'>alert('$message2');</script>"; redirect('resetpasswordcontroller/manage_news', 'refresh'); } }
my view:
<?php echo form_open_multipart('resetpasswordcontroller/news');?> <label>image</label> <input name = "userfile" type="file" /> <label> <input type="submit" name="submit" value="save , continue"> </label>
my model:
function update($id,$data){ $this->db->where('id', $id); $this->db->update('news',$data); }
my model function working because updating database nothing.i think problem view , controller.
when type var_dump($_files)
returns empty array means no file selected.
how can fix this?
$this->load->library('upload'); $config = array( 'upload_path' => './uploads/', 'allowed_types' => 'gif|jpg|png|pdf|doc|docx', 'max_size' => 10000, 'overwrite' => false ); $files = $_files; $cpt = count($_files['file']['name']); for($i=0; $i<$cpt; $i++) { $_files['userfile']['name'] = $files['file']['name'][$i]; $_files['userfile']['type'] = $files['file']['type'][$i]; $_files['userfile']['tmp_name'] = $files['file']['tmp_name'][$i]; $_files['userfile']['error'] = $files['file']['error'][$i]; $_files['userfile']['size'] = $files['file']['size'][$i]; $this->upload->initialize($config); $this->upload->do_upload(); <!-- insert db here ---> }
you can use reference works me.
Comments
Post a Comment