<% on error resume next dim upload,f_folder,file,formPath,iCount,filename,fileExt,filesizemin,filesizemax '****************************************************************** 'CheckFileType 函数用来检查文件是否为图片文件 '参数filename是本地文件的路径 '如果是文件jpeg,gif,bmp,png图片中的一种,函数返回true,否则返回false '****************************************************************** const adTypeBinary=1 dim jpg(1):jpg(0)=CByte(&HFF):jpg(1)=CByte(&HD8) dim bmp(1):bmp(0)=CByte(&H42):bmp(1)=CByte(&H4D) dim png(3):png(0)=CByte(&H89):png(1)=CByte(&H50):png(2)=CByte(&H4E):png(3)=CByte(&H47) dim gif(5):gif(0)=CByte(&H47):gif(1)=CByte(&H49):gif(2)=CByte(&H46):gif(3)=CByte(&H39):gif(4)=CByte(&H38):gif(5)=CByte(&H61) function CheckFileType(filename) CheckFileType=false dim fstream,fileExt,stamp,i fileExt=mid(filename,InStrRev(filename,".")+1) set fstream=Server.createobject("ADODB.Stream") fstream.Open fstream.Type=adTypeBinary fstream.LoadFromFile filename fstream.position=0 select case fileExt case "jpg","jpeg" stamp=fstream.read(2) for i=0 to 1 if ascB(MidB(stamp,i+1,1))=jpg(i) then CheckFileType=true else CheckFileType=false next case "gif" stamp=fstream.read(6) for i=0 to 5 if ascB(MidB(stamp,i+1,1))=gif(i) then CheckFileType=true else CheckFileType=false next case "png" stamp=fstream.read(4) for i=0 to 3 if ascB(MidB(stamp,i+1,1))=png(i) then CheckFileType=true else CheckFileType=false next case "bmp" stamp=fstream.read(2) for i=0 to 1 if ascB(MidB(stamp,i+1,1))=bmp(i) then CheckFileType=true else CheckFileType=false next end select fstream.Close set fseteam=nothing if err.number<>0 then CheckFileType=false end function function TrueStr(fileTrue) str_len=len(fileTrue) pos=Instr(fileTrue,chr(0)) if pos=0 or pos=str_len then TrueStr=true else TrueStr=false end if end function%> <% set upload=new upload_file if upload.form("act")="uploadfile" then filepath=trim(upload.form("filepath")) filelx=trim(upload.form("filelx")) i=0 for each formName in upload.File set file=upload.File(formName) fileExt=lcase(file.FileExt) '得到的文件扩展名不含有. if fileext<>"gif" and fileext<>"jpg" and fileext<>"swf" then response.write "文件格式不对! [ 重新上传 ]" response.end end if if file.filesize<100 then response.write "请先选择你要上传的文件! [ 重新上传 ]" response.end end if if (filelx<>"swf") and (filelx<>"jpg") then response.write "该文件类型不能上传! [ 重新上传 ]" response.end end if if filelx="swf" then if fileext<>"swf" then response.write "只能上传swf格式的Flash文件! [ 重新上传 ]" response.end end if end if if filelx="jpg" then if fileext<>"gif" and fileext<>"jpg" and fileext<>"swf" then response.write "只能上传jpg或gif格式的图片或SWF文件! [ 重新上传 ]" response.end end if end if if filelx="swf" then if file.filesize>(3000*1024) then response.write "最大只能上传 3M 的Flash文件! [ 重新上传 ]" response.end end if end if if filelx="jpg" then if fileext="swf" then if file.filesize>(2000*1024) then response.write "最大只能上传 2M 的Flash文件! [ 重新上传 ]" response.end end if else if file.filesize>(1000*1024) then response.write "最大只能上传 1000K 的图片文件! [ 重新上传 ]" response.end end if end if end if randomize ranNum=int(90000*rnd)+10000 filename=filepath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&ranNum&"."&fileExt %> <% if file.FileSize>0 then ''如果 FileSize > 0 说明有文件数据 'file.SaveAs Server.mappath(filename) ''保存文件 if fileext<>"swf" then if TrueStr(FileName)=false then response.write "非法文件" response.end end if file.SaveToFile Server.mappath(FileName) else file.SaveToFile Server.mappath(FileName) end if 'response.write file.FileName&"  上传成功!  
" 'response.write "新文件名:"&FileName&"
" 'response.write "新文件名已复制到所需的位置,可关闭窗口!" if filelx="swf" then response.write "" end if response.write "" %> <% end if set file=nothing next set upload=nothing end if %>