function
upload(
$files
,
$path
,
$type
=
array
(),
$max_size
=2097152){
if
(!
file_exists
(
$path
)||
is_file
(
$path
)){
if
(!@
mkdir
(
$path
)){
$result
['msg']='创建目录失败';
return
$result
;
}
}
$path
=rtrim(
$path
,'/');
if
(
$files
['size']>
$max_size
){
$result
['msg']='上传文件大小超出了PHP配置文件中upload_max_filesize限制';
return
$result
;
}
switch
(
$files
['error']){
case
0:
break
;
case
1:
$result
['msg']='上传文件大小超出了PHP配置文件中upload_max_filesize限制';
return
$result
;
case
2:
$result
['msg']='上传文件大小超出了表单中,MAX_FILE_SIZE指定的值';
return
$result
;
case
3:
$result
['msg']='文件上传不完整';
return
$result
;
case
4:
$result
['msg']='没有上传任何文件';
return
$result
;
case
6:
$result
['msg']='没有上传任何文件';
return
$result
;
default
:
$result
['msg']='未知错误';
return
$result
;
}
if
(!
is_uploaded_file
(
$files
['tmp_name'])){
$result
['msg']='文件不合法';
return
$result
;
}
$ext
=
array_pop
((
explode
('.',
$files
['name'])));
if
(
is_array
(
$type
)&&
count
(
$type
)>0){
if
(!in_array(
$ext
,
$type
)){
$result
['msg']='不允许此类型文件上传';
return
$result
;
}
}
$filename
=uniqid().'.'.
$ext
;
if
(!move_uploaded_file(
$files
['tmp_name'],
$path
.'/'.
$filename
)){
$result
['msg']='移动文件出错';
return
$result
;
}
$result
['msg']='上传成功';
$result
['name']=
$files
['name'];
$result
['size']=
$files
['size'];
$result
['filename']=
$filename
;
return
$result
;
}