2016-10-11 89 views
0

我想上传具有不同输入文件和数组名称的多个图像。如何在codeigniter中上传具有不同输入文件的多个图像

观点:

<form action="" enctype="multipart/form-data" method="post"> 
<input name="picture[]" class="form-control" style="padding-top: 0;" type="file"/> 
<input name="picture[]" class="form-control" style="padding-top: 0;" type="file"/> 
<input type='submit' value="upload" /> 
</form> 

控制器:

public function index($id=null) 
    { 
     $id = $this->input->get('id'); 
     if ($_POST) 
     { 
      if ($this->validation()) 
      { 
       $file = $this->upload_picture(); 
       if ($file['status'] == 'success') 
       { 
        echo $this->upload->data('file_name'); 
       } 
       else 
       { 
        echo $file['data']; 
        echo $file['status']; 
        $this->session->set_flashdata('alert', alert('error', $file['data'])); 
       } 
      } 
      else 
      { 
       $this->session->set_flashdata('alert', alert('error', validation_errors())); 
      } 
      //redirect($this->agent->referrer()); 
     } 
    } 

    private function upload_picture() 
    { 
     $config['upload_path']  = './assets/img/page/'; 
     $config['allowed_types'] = 'jpg|png|gif|jpeg'; 
     $config['max_size']   = 125000; // 1 GB 
     $config['encrypt_name']  = TRUE; 
     $this->load->library('upload', $config); 
     $this->upload->initialize($config); 
     if (! $this->upload->do_upload('picture[]')) 
     { 
      return array(
       'status' => 'error', 
       'data'  => $this->upload->display_errors() 
       ); 
     } 
     else 
     { 
      $data      = $this->upload->data(); 
      $resize['image_library'] = 'gd2'; 
      $resize['source_image']  = './assets/img/page/'.$data['file_name']; 
      $resize['maintain_ratio'] = TRUE; 
      // $resize['width']   = 1920; 
      // $resize['height']   = 1080; 
      $this->load->library('image_lib', $resize); 
      $this->image_lib->resize(); 
      return array(
       'status' => 'success', 
       'data'  => $this->upload->data() 
       ); 
     } 
    } 

    private function validation() 
    { 
     $this->load->library('form_validation'); 
     $this->form_validation->set_rules('picture[0]',   'Picture', 'trim'); 
     $this->form_validation->set_rules('picture[1]',   'Picture',   'trim'); 
     $this->form_validation->set_error_delimiters('', '<br>'); 
     return $this->form_validation->run(); 
    } 

结果在浏览器中始终显示错误意味着返回状态的upload_picture功能出错,我想的是加密的文件名和保存像数据库a4b8a0e070128b0a3dabd9e2931f7ae3.jpg not picture.jpg。

enter image description here

回答

0

笨上传类不以这种方式支持数组文件名。所以要么修改上传类文件(不建议修改核心类文件),要么修改代码。 可以命名像这样的投入:picture_1,picture_2等 在这种情况下,修改upload_picture()方法,像这样:

foreach($_FILES as $key=>$val){ 
    if(!$this->upload->do_upload($key)){ 
     $return[$key] = $this->upload->display_errors(); //store this in an array and return at the end. array structure is up to you 
    }else{ 
     $return[$key] = $this->upload->data(); //store this in an array and return at the end. array structure is up to you 
    } 
} 

return $return; // 

这样你通过一个使用循环上传文件之一。但是,您还必须修改主方法,因为它现在正在返回多维数组。我给你一个主意......

相关问题