问题
I have created a tab to add products while adding products you can add single product image as will be shown on main along with you can can also be able to upload multiple images along with the images the single image that will be featured now the problem is that single image is uploading correctly but it is saving the very first image and skipping the rest can anyone help me out with this 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(); ?>
回答1:
You can try like this:
According to your product-id or form-id is, it creates a folder for its name and stores multiple images in that folder.
if(!is_dir("uploads/gallery/".$id."/")) {
mkdir("uploads/gallery/".$id."/");
}
foreach($_FILES['gallery']['name'] as $key=>$val){
//upload and 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;
}
}
来源:https://stackoverflow.com/questions/45807076/unable-to-save-single-image-along-with-multiple-images-in-codeigniter