ASP文件操作函數(shù):圖片是否合法、文件和文件夾操作等
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
'函數(shù):檢測文件/文件夾是否存在 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 '函數(shù):刪除文件/文件夾 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 '函數(shù):獲取文件/文件夾信息 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'創(chuàng)建時間 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'創(chuàng)建時間 tmp(4) = fd.datelastmodified'最后修改時間 end if set fso = nothing file_info = tmp end function '函數(shù):復制文件/文件夾 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 '函數(shù):創(chuàng)建文件夾 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 '函數(shù):獲取指定目錄下所有文件及文件夾列表 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,"*")'返回長度為二的字符數(shù)組 end function '函數(shù):返回圖片類型及尺寸 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 '函數(shù):檢測圖片文件合法性 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 '函數(shù):采集遠程文件并保存到本地磁盤 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 '函數(shù):讀取文件內(nèi)容到字符串 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 '函數(shù):保存字符串到文件 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 '函數(shù):讀取ASP類型文件的全部內(nèi)容
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 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |