我创建了一个用于添加产品的标签,同时添加了可以添加单个产品图像的产品,这将显示在主屏幕上,您还可以将多个图像与图像一起上传到将要显示的单个图像问题是单个图像正确上传,但它保存了第一个图像,跳过其余图像可以帮助我解决这个问题
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');
}
<?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(); ?>
答案 0 :(得分:1)
您可以尝试这样:
根据您的product-id或form-id,它会为其名称创建一个文件夹,并在该文件夹中存储多个图像。
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;
}
}