百度空间 | 百度首页 
 
查看文章
 
删除文章并同时删除文件
2009年08月01日 星期六 下午 05:48

  当我们在使用ewebeditor,meditor等在线编辑器的时候,因上传的图片文件无法很好的得到管理,大量文章删除后图片还留在空间中,这样就造成了网站空间的浪费。  如果能在删除文章的同时删除上传的文件,上面的问题将彻底解决,可谓是一劳永逸。下面的方法,就是为此而设计的。  通过正则表达式提取文章中的图片路径,然后将其一一删除。代码如下:

sub delImage(tTopicid)'参数文章ID
'使用正则查找文章中全部图片路径
set delFilers=server.createobject("adodb.recordset")
delFilesql="select top 1 content from team_topic where topicid="&tTopicid'注意表名和字段名
delFilers.open delFilesql,conn,1,1
AA=delFilers(0)
delFilers.close
Set delFilers=nothing
Set RE=NEW REGEXP
RE.GLOBAL=TRUE
'RE.PATTERN="<IMG[^<]{,1}>"

'匹配图片路径的正则表达式
RE.Pattern = "(src=)('|"&CHR(34)&"| )?(.[^'| |"&CHR(34)&"]*)(\.)(jpg|gif|png|bmp|jpeg)('|"&CHR(34)&"| |>)?" '
Set Matches = RE.Execute(AA) ' 执行搜索。
For Each Match in Matches
   if instr(Match,"../upload/")>0 then
    allpicurl=Replace(MATCH,"src=","")
    '去除双引号
    allpicurl=replace(allpicurl,"'","")
    allpicurl=replace(allpicurl,CHR(34),"")
    allpicurl=replace(allpicurl,"../","") '只取本地上传目录里的图片
    deletefile(server.mapPath(allpicurl))
   end if
Next
end sub
'文件删除函数
function deletefile(filename)
on error resume next
if filename<>"" then
   set fso=server.CreateObject("scripting.filesystemobject")
   if fso.FileExists(filename) then
    fso.DeleteFile filename
   'else
    'Response.Write "<script>alert('该文件不存在');</script>"
   end if
end if
end function


类别:web开发 | 添加到搜藏 | 浏览() | 评论 (0)
 
最近读者:
 
网友评论:
发表评论:
姓 名:
网址或邮箱: (选填)
内 容:
验证码: 请点击后输入四位验证码,字母不区分大小写
      

     

©2009 Baidu