php - How to add underscore before index? -
i want insert underscore before counting index every time upload multiple files.
what expect:
schools.jpg
schools_1.jpg
schools_2.jpg
schools_3.jpg
my program output:
schools.jpg
schools1.jpg
schools2.jpg
schools3.jpg
here's code:
controller
public function do_upload() { $this->load->library('upload'); $files = $_files; $cpt = count($_files['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_files['userfile']['name']= $files['userfile']['name'][$i]; $_files['userfile']['type']= $files['userfile']['type'][$i]; $_files['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_files['userfile']['error']= $files['userfile']['error'][$i]; $_files['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->db->insert('tbl_image', $file_array); } } private function set_upload_options() { $config = array( 'image_library' => 'gd2', 'file_name' => $this->input->post('file_name'), 'upload_path' => './public/img/uploads', 'upload_url' => base_url().'public/img/uploads', 'allowed_types' => 'jpg|jpeg|pdf', 'max_size' => '1024kb', 'max_width' => '1024', 'max_height' => '768', 'maintain_ratio'=> true, 'overwrite' => false, ); return $config; }
html
<label for="file">select file upload:</label> <input type="file" name="userfile[]" multiple/> <label for="file_name">set image file name:</label> <input type="text" size="20" name="file_name" required id="file_name"/> <input type="submit" id="btnsubmit" value="upload file"/>
i saw lot of wrong suggested answer, put underscore in index of array, this
$_files['userfile']['name']= $files['userfile']['name'][$i]."_".[$i]; //you can't because $file['userfile']['name'][$i] array better code $flag=0; for($i=0; $i<$cpt; $i++) { $flag++; $_files['userfile']['name']=$files['userfile']['name'][$i]; $_files['userfile']['type']= $files['userfile']['type'][$i]; $_files['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_files['userfile']['error']= $files['userfile']['error'][$i]; $_files['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options($i, $flag)); $this->db->insert('tbl_image', $file_array); } private function set_upload_options($i, $f) { $charac= ""; if($f>1) { $charac=$this->input->post('file_name')."_".$i; //here code add underscore image 2 , above } else { $charac=$this->input->post('file_name'); } $config = array( 'image_library' => 'gd2', 'file_name' => $charac, 'upload_path' => './public/img/uploads', 'upload_url' => base_url().'public/img/uploads', 'allowed_types' => 'gif|jpg|jpeg', 'max_size' => '1024kb', 'max_width' => '1024', 'max_height' => '768', 'maintain_ratio'=> true, 'overwrite' => false, ); return $config; }
Comments
Post a Comment