无法在codeigniter中保存单个图像和多个图像 [英] unable to save single image along with multiple images in codeigniter

查看:146
本文介绍了无法在codeigniter中保存单个图像和多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用于添加产品的标签,同时添加了可以添加单个产品图像的产品,这将显示在主屏幕上,您还可以将多个图像与图像一起上传到将要显示的单个图像问题是单个图像正在正确上传,但它正在保存第一个图像并跳过其余图像任何人都可以帮助我解决这个问题

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

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(); ?>


推荐答案

您可以尝试这样:

根据您的product-id或form-id,它会为其名称创建一个文件夹,并在该文件夹中存储多个图像。

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;
  }
}

这篇关于无法在codeigniter中保存单个图像和多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆