ASP文件操作函数:图片是否合法、文件和文件夹操作等
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
'函数:检测文件/文件夹是否存在 function file_exists(path) dim tmp : tmp = false dim fso : set fso = server.CreateObject(OBJ_FSO) if fso.fileexists(server.MapPath(path)) then tmp = true if fso.folderexists(server.MapPath(path)) then tmp = true set fso = nothing file_exists = tmp end function '函数:删除文件/文件夹 function file_delete(path) dim tmp : tmp = false dim fso : set fso=server.CreateObject(OBJ_FSO) if fso.fileexists(server.MapPath(path)) then'目标是文件 fso.deletefile(server.MapPath(path)) if not fso.fileexists(server.MapPath(path)) then tmp = true end if if fso.folderexists(server.MapPath(path)) then'目标是文件夹 fso.deletefolder(server.MapPath(path)) if not fso.folderexists(server.MapPath(path)) then tmp = true end if set fso = nothing file_delete = tmp end function '函数:获取文件/文件夹信息 function file_info(path) dim tmp(4) dim fso : set fso = server.CreateObject(OBJ_FSO) if fso.fileexists(server.MapPath(path)) then '目标是文件 dim fl : set fl = fso.getfile(server.MapPath(path)) tmp(0) = fl.type'类型 tmp(1) = fl.attributes'属性 tmp(2) = csize(fl.size,4)'大小 tmp(3) = fl.datecreated'创建时间 tmp(4) = fl.datelastmodified'最后修改时间 elseif fso.folderexists(server.MapPath(path)) then '目标是文件夹 dim fd : set fd = fso.getfolder(server.MapPath(path)) tmp(0) = "folder"'类型 tmp(1) = fd.attributes'属性 tmp(2) = csize(fd.size,4)'大小 tmp(3) = fd.datecreated'创建时间 tmp(4) = fd.datelastmodified'最后修改时间 end if set fso = nothing file_info = tmp end function '函数:复制文件/文件夹 function file_copy(file_start,file_end,model) if model<>0 and model<>1 then model = false else model = cbool(model) dim tmp : tmp = false dim fso : set fso = server.CreateObject(OBJ_FSO) if fso.fileexists(server.MapPath(file_start)) then '目标是文件 fso.copyfile server.MapPath(file_start),server.MapPath(file_end),model if fso.fileexists(server.MapPath(file_end)) then tmp = true end if if fso.folderexists(server.MapPath(file_start)) then '目标是文件夹 fso.copyfolder server.MapPath(file_start),server.MapPath(file_end),model if fso.folderexists(server.MapPath(file_end)) then tmp = true end if set fso = nothing file_copy = tmp end function '函数:创建文件夹 function file_create(path,model) if model<>0 and model<>1 then model = false else model = cbool(model) dim tmp : tmp = false dim fso : set fso = server.CreateObject(OBJ_FSO) if fso.folderexists(server.MapPath(path)) then if model then fso.deletefolder(server.MapPath(path)) : fso.createfolder server.MapPath(path) else fso.createfolder server.MapPath(path) end if if fso.folderexists(server.MapPath(path)) then tmp = true set fso = nothing file_create = tmp end function '函数:获取指定目录下所有文件及文件夹列表 function file_list(path) if not file_exists(path) then file_list=array("","") : exit function dim fso : set fso = server.CreateObject(OBJ_FSO) dim fdr : set fdr = fso.getfolder( server.MapPath(path) ) dim folders : set folders = fdr.subfolders dim f, t, tmp : t = "" : tmp = "" for each f in folders tmp = tmp & t & f.name : t = "|" next tmp = tmp & "*" : t = "" dim files : set files = fdr.files for each f in files tmp = tmp & t & f.name : t = "|" next set fso = nothing file_list = split(tmp,"*")'返回长度为二的字符数组 end function '函数:返回图片类型及尺寸 function file_imginfo(path) dim tmp : tmp = array("",0,0) dim fso : set fso = server.CreateObject(OBJ_FSO) if fso.fileexists(server.MapPath(path)) then dim img : set img = loadpicture(server.MapPath(path)) select case img.type case 0 : tmp(0) = "none"'类型 case 1 : tmp(0) = "bitmap" case 2 : tmp(0) = "metafile" case 3 : tmp(0) = "ico" case 4 : tmp(0) = "win32-enhanced metafile" end select tmp(1) = round(img.width/26.4583)'宽度 tmp(2) = round(img.height/26.4583)'高度 set img = nothing set fso = nothing end if file_imginfo = tmp end function '函数:检测图片文件合法性 function file_isimg(path) dim tmp : tmp = false if not file_exists(path) then file_isimg = tmp : exit function 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) dim fstream,fext,stamp,i fext = mid(path, instrrev(path,".")+1) set fstream = server.CreateObject(OBJ_STRM) fstream.open fstream.type = 1 fstream.loadfromfile server.MapPath(path) fstream.position = 0 select case fext case "jpg","jpeg": stamp = fstream.read(2) for i=0 to 1 if ascb(midb(stamp,i+1,1))=jpg(i) then tmp=true else tmp=false next case "gif": stamp = fstream.read(6) for i=0 to 5 if ascb(midb(stamp,i+1,1))=gif(i) then tmp=true else tmp=false next case "png": stamp = fstream.read(4) for i=0 to 3 if ascb(midb(stamp,i+1,1))=png(i) then tmp=true else tmp=false next case "bmp": stamp = fstream.read(2) for i=0 to 1 if ascb(midb(stamp,i+1,1))=bmp(i) then tmp=true else tmp=false next end select fstream.close : set fstream = nothing file_isimg = tmp end function '函数:采集远程文件并保存到本地磁盘 function file_savefromurl(fileurl,savepath,savetype) if savetype<>1 and savetype<>2 then savetype=2 dim xmlhttp : set xmlhttp = server.CreateObject(OBJ_XHTP) with xmlhttp .open "get", fileurl, false, "", "" .send() dim fl : fl = .responsebody end with set xmlhttp = nothing dim stream : set stream = server.CreateObject(OBJ_STRM) with stream .type = savetype .open .write fl 'if savetype=1 then .write fl else .writetext fl .savetofile server.MapPath(savepath), 2 .cancel() .close() end with set stream = nothing file_savefromurl = file_exists(savepath) end function '函数:读取文件内容到字符串 function file_read(path) dim tmp : tmp = "false" if not file_exists(path) then file_read = tmp : exit function dim stream : set stream = server.CreateObject(OBJ_STRM) with stream .type = 2 '文本类型 .mode = 3 '读写模式 .charset = "utf-8" .open .loadfromfile(server.MapPath(path)) tmp = .readtext() end with stream.close : set stream = nothing file_read = tmp end function '函数:保存字符串到文件 function file_save(str,path,model) if model<>0 and model<>1 then model=1 if model=0 and file_exists(path) then file_save=true : exit function dim stream : set stream = server.CreateObject(OBJ_STRM) with stream .type = 2 '文本类型 .charset = "utf-8" .open .writetext str .savetofile(server.MapPath(path)),model+1 end with stream.close : set stream = nothing file_save = file_exists(path) end function '函数:读取ASP类型文件的全部内容
function file_iread(path) dim str : str = file_read(path) dim pattern : pattern = "<\!--#include[ ]+?file[ ]*?=[ ]*?""(\S+?)""--\>" dim matches : set matches = str_execute(pattern,str) dim m, f, tmp for each m in matches f = mid(path,1,instrrev(path,"/"))&m.submatches(0) tmp = file_read(f) if str_test(pattern,tmp) then tmp = file_iread(f) '处理子包含 str = replace(str,m.value,tmp) next pattern = "<%@[ ]*?LANGUAGE[ ]*?=[ ]*?""[a-zA-Z]+?""[ ]+?CODEPAGE[ ]*?=[ ]*?""[0-9]+?""[ ]*?%\>" str = str_replace(pattern,str,"") file_iread = str end function 该文章在 2011/2/26 18:04:05 编辑过 |
关键字查询
相关文章
正在查询... |