找到上传文件类,ThinkPHP/Library/Think/Upload.class.php,找到dealFiles
方法
/**
* 转换上传文件数组变量为正确的方式
* @access private
* @param array $files 上传的文件变量
* @return array
*/
private function dealFiles($files) {
$fileArray = array();
$n = 0;
foreach ($files as $key=>$file){
if(is_array($file['name'])) {
$keys = array_keys($file);
$count = count($file['name']);
for ($i=0; $i<$count; $i++) {
$fileArray[$n]['key'] = $key;
foreach ($keys as $_key){
$fileArray[$n][$_key] = $file[$_key][$i];
}
$n++;
}
}else{
$fileArray = $files;
break;
}
}
return $fileArray;
}
分析到这个方法处理文件是有问题的。上传多个文件时,传入$files 的值为
array(5) {
["name"]=>
array(4) {
[0]=>
string(9) "组 1.png"
[1]=>
string(9) "组 2.png"
[2]=>
string(9) "组 3.png"
[3]=>
string(0) ""
}
["type"]=>
array(4) {
[0]=>
string(9) "image/png"
[1]=>
string(9) "image/png"
[2]=>
string(9) "image/png"
[3]=>
string(0) ""
}
["tmp_name"]=>
array(4) {
[0]=>
string(25) "C:\wamp64\tmp\phpE05E.tmp"
[1]=>
string(25) "C:\wamp64\tmp\phpE06F.tmp"
[2]=>
string(25) "C:\wamp64\tmp\phpE07F.tmp"
[3]=>
string(0) ""
}
["error"]=>
array(4) {
[0]=>
int(0)
[1]=>
int(0)
[2]=>
int(0)
[3]=>
int(4)
}
["size"]=>
array(4) {
[0]=>
int(19604)
[1]=>
int(28297)
[2]=>
int(35891)
[3]=>
int(0)
}
}
按道理来说此处应该是将这个数据处理成如下形式:
array(4) {
[0]=>
array(5) {
["name"]=>
string(9) "组 1.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(25) "C:\wamp64\tmp\php9212.tmp"
["error"]=>
int(0)
["size"]=>
int(19604)
}
[1]=>
array(5) {
["name"]=>
string(9) "组 2.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(25) "C:\wamp64\tmp\php9213.tmp"
["error"]=>
int(0)
["size"]=>
int(28297)
}
[2]=>
array(5) {
["name"]=>
string(9) "组 3.png"
["type"]=>
string(9) "image/png"
["tmp_name"]=>
string(25) "C:\wamp64\tmp\php9223.tmp"
["error"]=>
int(0)
["size"]=>
int(35891)
}
[3]=>
array(5) {
["name"]=>
string(0) ""
["type"]=>
string(0) ""
["tmp_name"]=>
string(0) ""
["error"]=>
int(4)
["size"]=>
int(0)
}
}
仔细分析此方法,并且修改这个方法如下,可修复“非法上传文件!”这个错误:
private function dealFiles($files) {
$fileArray = array();
$n = 0;
if(is_array($files['name'])) {
$keys = array_keys($files);
foreach ($files['name'] as $key => $value) {
foreach ($keys as $_key){
$fileArray[$n][$_key] = $files[$_key][$key];
}
$n++;
}
}else{
$fileArray = $files; // 单个文件上传
}
return $fileArray;
}