CI多文件上传

Posted 阳雪凌空

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CI多文件上传相关的知识,希望对你有一定的参考价值。

CI官网手册上面写道,文件上传:

function getUploadName($field="prize_file")
{
$config[‘upload_path‘] = ‘uploads/prizepath/‘;
$config[‘allowed_types‘] = ‘gif|jpg|png|swf|bmp|txt‘;
$config[‘file_name‘] = date(‘Ymdhis‘).rand(100, 999);
$config[‘max_size‘] = ‘1024‘;
$config[‘max_width‘] = ‘0‘;
$config[‘max_height‘] = ‘0‘;

$this->load->library(‘upload‘, $config);

if(!dir_create($config[‘upload_path‘]))
{
exit(‘上传目录出错‘);
}

if( ! $this->upload->do_upload($field))
{
$data = array(‘error‘ => $this->upload->display_errors());
}
else
{
$data = array(‘upload_data‘ => $this->upload->data());
}

return $data;
}

这是单文件上传,文件的格式是:
array (size=5)
  ‘name‘ => 

string

 ‘640-300-50k.jpg‘ (length=15)
  ‘type‘ => 

string

 ‘image/jpeg‘ (length=10)
  ‘tmp_name‘ => 

string

 ‘E:\wamp\tmp\php9A91.tmp‘ (length=23)
  ‘error‘ => 

int

 0
  ‘size‘ => 

int

 50953
如果要多文件上传呢:
文件的格式是:
array (size=5)
  ‘name‘ => 
    array (size=4)
      0 => 
string
 ‘640-300-50k.jpg‘ (length=15)
      1 => 
string
 ‘20160727051524365.jpg‘ (length=21)
      2 => 
string
 ‘‘ (length=0)
      3 => 
string
 ‘‘ (length=0)
  ‘type‘ => 
    array (size=4)
      0 => 
string
 ‘image/jpeg‘ (length=10)
      1 => 
string
 ‘image/jpeg‘ (length=10)
      2 => 
string
 ‘‘ (length=0)
      3 => 
string
 ‘‘ (length=0)
  ‘tmp_name‘ => 
    array (size=4)
      0 => 
string
 ‘E:\wamp\tmp\php79C9.tmp‘ (length=23)
      1 => 
string
 ‘E:\wamp\tmp\php79CA.tmp‘ (length=23)
      2 => 
string
 ‘‘ (length=0)
      3 => 
string
 ‘‘ (length=0)
  ‘error‘ => 
    array (size=4)
      0 => 
int
 0
      1 => 
int
 0
      2 => 
int
 4
      3 => 
int
 4
  ‘size‘ => 
    array (size=4)
      0 => 
int
 50953
      1 => 
int
 42920
      2 => 
int
 0
      3 => 
int
 0

这个时候最关键的思路就是:
把多文件上传,组装成单文件上传的格式,并且,每个文件的名字要不同,附上代码:
foreach($_FILES[‘prize_file‘][‘error‘] as $k=>$v){
if($v!=4){
$field="prize_file".$k; ----------(这一步很关键)
$_FILES[$field]=array("name"=>$_FILES["prize_file"][‘name‘][$k],"type"=>$_FILES[‘prize_file‘][‘type‘][$k],"tmp_name"=>$_FILES[‘prize_file‘][‘tmp_name‘][$k],"error"=>$_FILES[‘prize_file‘][‘error‘][$k],"size"=>$_FILES[‘prize_file‘][‘size‘][$k]);
$data = $this->getUploadName($field); -------(这个时候,每个文件上传的名字就不同了)
$upload_data[]=$data;

}
}



 

以上是关于CI多文件上传的主要内容,如果未能解决你的问题,请参考以下文章

java Ftp上传创建多层文件的代码片段

030.CI4框架CodeIgniter, 文件的上传与移动

ci 文件类型在禁止上传之列

Alamofire 文件上传出现错误“JSON 文本未以数组或对象开头,并且允许未设置片段的选项”

CI(持续集成)CD(持续交付)

如何用建木CI往七牛云上传文件