<?php
function
UpLoadFileOne(
$input
,
$path
=
'upload'
,
$ftype
=
'gif,jpg,png'
,
$fsize
=2){
if
(
strrpos
(
$path
,
'/'
) <
strlen
(
$path
)-1)
$path
.=
'/'
;
$Atype
=
explode
(
','
,
$ftype
);
$fsize
=
$fsize
*1048576;
$fileInfo
=
$_FILES
[
$input
];
$name
=
$fileInfo
[
'name'
];
$size
=
$fileInfo
[
'size'
];
$type
=
$fileInfo
[
'type'
];
$tmp_name
=
$fileInfo
[
'tmp_name'
];
$error
=
$fileInfo
[
'error'
];
if
(
$error
== 0){
$type
= MyFileType(
$type
);
$myfile
= CreatMyFile(
$path
);
if
(
$myfile
==false)
return
false;
else
$path
=
$myfile
.MakeFname(
$type
);
if
(in_array(
$type
,
$Atype
) &&
$size
<=
$fsize
&&
is_uploaded_file
(
$fileInfo
[
'tmp_name'
])){
if
(@move_uploaded_file(
$tmp_name
,
$path
))
return
str_replace
(
array
(
'../'
,
'./'
),
''
,
$path
);
else
return
false;
}
else
return
false;
}
else
return
false;
}
function
UpLoadFileAll(
$input
=
'UpPic'
,
$path
=
'upload'
,
$ftype
=
'jpg,gif,png'
,
$fsize
=2){
$fileInfo
=
$_FILES
[
$input
];
if
(
strrpos
(
$path
,
'/'
) <
strlen
(
$path
)-1)
$path
.=
'/'
;
$myfile
= CreatMyFile(
$path
);
if
(
$myfile
==false)
return
false;
$Atype
=
explode
(
','
,
$ftype
);
$fsize
=
$fsize
*1048576;
$js
=
"以下文件上传成功:\\n\\n"
;
if
(
is_array
(
$fileInfo
[
"error"
])){
foreach
(
$fileInfo
[
"error"
]
as
$key
=>
$error
){
if
(
$error
== 0) {
$name
=
$fileInfo
[
"name"
][
$key
];
$size
=
$fileInfo
[
"size"
][
$key
];
$type
=
$fileInfo
[
"type"
][
$key
];
$tmp_name
=
$fileInfo
[
"tmp_name"
][
$key
];
$type
= MyFileType(
$type
);
$path
=
$myfile
.MakeFname(
$type
);
if
(in_array(
$type
,
$Atype
) &&
$size
<=
$fsize
){
if
(@move_uploaded_file(
$tmp_name
,
$path
)){
$array
[] =
$path
;
$js
.=
" "
.
$name
.
" 上传成功 !\\n"
;
}
}
}
}
}
echo
""
;
return
$array
;
}
function
ResizeImage(
$path
,
$maxwidth
,
$maxheight
){
$picS
=
substr
(
$path
, -3);
$name
=
substr
(
$path
, 0,
strrpos
(
$path
,
'.'
)).
'_S'
;
switch
(
$picS
){
case
'jpg'
:
$im
= @imagecreatefromjpeg(
$path
);
break
;
case
'gif'
:
$im
= @imagecreatefromgif(
$path
);
break
;
default
:
$im
= @imagecreatefrompng(
$path
);
}
$width
= imagesx(
$im
);
$height
= imagesy(
$im
);
if
((
$maxwidth
&&
$width
>
$maxwidth
) || (
$maxheight
&&
$height
>
$maxheight
)){
if
(
$maxwidth
&&
$width
>
$maxwidth
){
$widthratio
=
$maxwidth
/
$width
;
$RESIZEWIDTH
=true;
}
if
(
$maxheight
&&
$height
>
$maxheight
){
$heightratio
=
$maxheight
/
$height
;
$RESIZEHEIGHT
=true;
}
if
(
$RESIZEWIDTH
&&
$RESIZEHEIGHT
){
if
(
$widthratio
<
$heightratio
){
$ratio
=
$widthratio
;
}
else
{
$ratio
=
$heightratio
;
}
}
elseif
(
$RESIZEWIDTH
){
$ratio
=
$widthratio
;
}
elseif
(
$RESIZEHEIGHT
){
$ratio
=
$heightratio
;
}
$newwidth
=
$width
*
$ratio
;
$newheight
=
$height
*
$ratio
;
if
(function_exists(
"imagecopyresampled"
)){
$newim
= imagecreatetruecolor(
$newwidth
,
$newheight
);
imagecopyresampled(
$newim
,
$im
, 0, 0, 0, 0,
$newwidth
,
$newheight
,
$width
,
$height
);
}
else
{
$newim
= imagecreate(
$newwidth
,
$newheight
);
imagecopyresized(
$newim
,
$im
, 0, 0, 0, 0,
$newwidth
,
$newheight
,
$width
,
$height
);
}
}
else
{
$newim
=
$im
;
}
switch
(
$picS
){
case
'jpg'
:
$PicPath
=
$name
.
".jpg"
;
if
(imagejpeg(
$newim
,
$PicPath
)){
imagedestroy(
$newim
);
return
str_replace
(
array
(
'../'
,
'./'
),
''
,
$PicPath
);
}
else
{
return
false;
}
break
;
case
'gif'
:
$PicPath
=
$name
.
".gif"
;
if
(imagegif(
$newim
,
$PicPath
)){
imagedestroy(
$newim
);
return
str_replace
(
array
(
'../'
,
'./'
),
''
,
$PicPath
);
}
else
{
return
false;
}
break
;
default
:
$PicPath
=
$name
.
".png"
;
if
(imagepng(
$newim
,
$PicPath
)){
imagedestroy(
$newim
);
return
str_replace
(
array
(
'../'
,
'./'
),
''
,
$PicPath
);
}
else
{
return
false;
}
}
}
function
MyFileType(
$type
) {
$type
=
strtolower
(
$type
);
switch
(
$type
) {
case
'application/msword'
:
$type
=
'doc'
;
break
;
case
'application/vnd.ms-excel'
:
$type
=
'xls'
;
break
;
case
'application/vnd.ms-powerpoint'
:
$type
=
'ppt'
;
break
;
case
'application/octet-stream'
:
$type
=
'rar'
;
break
;
case
'text/plain'
:
$type
=
'txt'
;
break
;
case
'image/pjpeg'
:
$type
=
'jpg'
;
break
;
case
'image/gif'
:
$type
=
'gif'
;
break
;
case
'image/x-png'
:
$type
=
'png'
;
break
;
case
'image/bmp'
:
$type
=
'bmp'
;
break
;
default
:
$type
=
'err'
;
}
return
$type
;
}
function
CreatMyFile(
$fname
=
''
){
switch
(
$fname
){
case
''
:
break
;
default
:
if
(
strrpos
(
$fname
,
'/'
) <
strlen
(
$fname
)-1)
$fname
.=
'/'
;
}
$fname
.=
date
(
"Y-m"
);
if
(
is_dir
(
$fname
))
return
$fname
.
'/'
;
if
(
mkdir
(
$fname
, 0755)==false)
return
false;
return
$fname
.
'/'
;
}
function
MakeFname(
$ftype
) {
$fname
=
date
(
"mdHis"
).
'_'
.rand(100000, 999999);
return
$fname
.
'.'
.
$ftype
;
}
?>