php - unable to save single image along with multiple images in codeigniter -
i have created tab add products while adding products can add single product image shown on main along can can able upload multiple images along images single image featured problem single image uploading correctly saving first image , skipping rest can me out concern
controller:
public function addproduct() { $config['upload_path'] = getcwd().'/assets/uploads/products/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '10000'; $this->load->library('upload', $config); if (!$this->upload->do_upload('profile_picture') && $this->form_validation->run() == false) { $error = $this->upload->display_errors(); $this->session->set_flashdata('success_msg', validation_errors() . $error); redirect('admin/products?add=1', $session_data); } else { $data = $this->upload->data(); $data = array( 'name' => $this->input->post('name'), 'price' => $this->input->post('price'), 'featured' => $this->input->post('ft'), 'category' => $this->input->post('category'), 'description' => $this->input->post('description'), 'in_stock' => $this->input->post('in_stock'), 'meta_tags' => $this->input->post('meta_tags'), 'meta_description' => $this->input->post('meta_description'), 'picture' => $data['file_name'] ); $prod_id = $this->data_insert->addproduct($data); $filescount = count($_files['gallery']['name']); for($i = 0; $i < $filescount; $i++){ $_files['gallery']['name'] = $_files['gallery']['name'][$i]; $_files['gallery']['type'] = $_files['gallery']['type'][$i]; $_files['gallery']['tmp_name'] = $_files['gallery']['tmp_name'][$i]; $_files['gallery']['error'] = $_files['gallery']['error'][$i]; $_files['gallery']['size'] = $_files['gallery']['size'][$i]; $uploadpath = getcwd().'/assets/uploads/products/'; $config1['upload_path'] = $uploadpath; $config1['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload', $config1); $this->upload->initialize($config1); if($this->upload->do_upload('gallery')){ $filedata = $this->upload->data(); $uploaddata[$i]['file_name'] = $filedata['file_name']; $dataupload = array( "post_id" => $prod_id, "post_img" => $uploaddata[$i]['file_name'], "type" => 'product' ); $insert = $this->data_insert->insertgallery($dataupload); } } $this->session->set_flashdata('success_msg', 'product added successfully'); redirect('admin/products'); }
view:
<?php echo form_open_multipart('admin/addproduct'); ?> <div class="col-md-4"> <div class="form-group"> <label>featured image</label> <input type="file" name='profile_picture' /> </div> </div> <div class="col-md-4"> <div class="form-group"> <label>gallery image</label> <input type="file" name='gallery[]' multiple /> </div> </div> <div class="form-group" style="text-align:right;"> <?php echo anchor('admin/players', 'back', "class='bt btn-info btn-anchor anchor2'"); ?> <?php echo form_submit('updatepage', 'add product', "class='btn btn-info'"); ?> </div> <?php echo form_close(); ?>
you can try this:
according product-id or form-id is, creates folder name , stores multiple images in folder.
if(!is_dir("uploads/gallery/".$id."/")) { mkdir("uploads/gallery/".$id."/"); } foreach($_files['gallery']['name'] $key=>$val){ //upload , stored images $target_dir = "uploads/gallery/".$id."/"; $target_file = $target_dir.$_files['gallery']['name'][$key]; $type = 'image'; if(move_uploaded_file($_files['gallery']['tmp_name'][$key],$target_file)){ $this->model->addgallery($id,$target_file,$type,$thumbnail); //$images_arr[] = $target_file; } }
wiki
Comments
Post a Comment