CI多文件上傳

CI官網手冊上面寫道,文件上傳:php

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' => 

stringphp7

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

stringthis

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

stringspa

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

intdebug

 0
  'size' => 

intget

 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; }}
相關文章
相關標籤/搜索