如何使用codeigniter上传多个图像并显示相关图像

时间:2016-02-25 09:00:22

标签: php mysql codeigniter

我想上传多张图片并显示相关图片,但是我收到错误,你没有选择任何要上传的文件 我的观点页是这个

<div class="form-group col-md-12">
          <label class="form-group col-md-3" for="exampleInputEmail1">Image</label> 

              <div class="form-group col-md-6">
              <input type="file" class="" id="image" name="image[]">
            </div>
      </div>

我的控制器看起来像这样......

public function add_images()
{
 $data['active_mn']='add_images';
if($_POST)
{

  $this->form_validation->set_rules('title','title','required');
  $this->form_validation->set_rules('parentname','parent category','required');
  $this->form_validation->set_rules('subname','sub category','required');
  $this->form_validation->set_rules('code','code','required');
  $this->form_validation->set_rules('description','Description','required');
  $this->form_validation->set_rules('size','Size','required');      

  if($this->form_validation->run()==true)
  {
    if($this->do_upload())
    {
      $stat=$this->roxmodel->add_images($this->upload->file_name);
      if($stat)
      {
        $data=array();
        $colors=array_filter($this->session->userdata('colors'));
        /*creating a multidiemensional array to batch insert*/
        foreach ($colors as $color) 
        {
          $new_array=array('product_id'=>$stat,'color'=>$color);
          array_push($data, $new_array);
        }

        $product=$this->roxmodel->add_product_color($data);
        $this->session->unset_userdata('colors');
        $this->session->set_flashdata('message', 'Gallery added Successfully.');
        redirect('admin_control/view_images');

      }
      else
        {
          $this->session->set_flashdata('message', 'Insertion Failed.');
          redirect('admin_control/add_images');
        }
        //redirect('admin_control/view_images');
    }


  }

}

$data['parent']=$this->roxmodel->get_parentcategory();
$this->load->view('add_images',$data);

}

我上传的路径看起来像这样..

function image_upload()

{

$name_array=array(); 
$count = count($_FILES['image']['size']);
foreach($_FILES as $key => $value)
{
for ($s = 0; $s < $count; $s++)
  {
  $config['upload_path'] = './images/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '5000';
  $config['max_width'] = '4000';
  $config['max_height'] = '6500';
  $config['file_name'] ='img'.time();
  $this->load->library('upload', $config);
  if(! $this->upload->do_upload("image"))
  {
    $this->session->set_flashdata('message',$this->upload->display_errors());
    return false;
  }
  else
  {
      $config['image_library'] = 'gd2';
      $config['source_image'] = './images/'.$this->upload->file_name;
      $config['create_thumb'] = TRUE;
      $config['maintain_ratio'] = TRUE;
      $config['width'] = 540;
      $config['height'] = 660;
      $config['new_image'] = './images/thumb/';

$this->load->library('image_lib', $config);

if(! $this->image_lib->resize())
    {
      $this->session->set_flashdata('message',$this->image_lib->display_errors());
      return false;
    }
    else
    {
      return true;
    }
  }
}


}

}

2 个答案:

答案 0 :(得分:1)

尝试上传多张图片.. ,请在表单中添加 enctype =&#39; multipart / form-data&#39;

$name_array=array(); 
$count = count($_FILES['image']['size']);
foreach($_FILES as $key => $value)
{
    for ($s = 0; $s < $count; $s++)
    {
$_FILES['userfile']['name'] = $value['name'][$s];
        $_FILES['userfile']['type'] = $value['type'][$s];
        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['userfile']['error'] = $value['error'][$s];
        $_FILES['userfile']['size'] = $value['size'][$s];
        $config['upload_path'] = "uploads/item_images/itemimage/original/";//your imagepath here
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '8000';
        $config['max_width'] = '10240';
        $config['max_height'] = '7680';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload("image"))
        {
            $data = $this->upload->data();
          if ($this->image_moo->errors)
            {
                print $this->upload->display_errors();
            }
            else
            {
                $name_array[] = $data['file_name'];

            }  } }

答案 1 :(得分:0)

默认情况下,codeIgniter不支持多文件上传。所以你可以使用这个库    CodeIgniter Multiple Upload Library