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