防止用户上传产生无效文件源码
类别: ASP教程
经过近日来的调试,狂人于4月12日写的一篇日志“用户上传产生无效文件的解决思路”代码部分已经完成。欢迎批评指正,以便改进。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
[FileName]:upload.asp
[Code]:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#i nclude virtual="upload.inc"-->
<%
If Request.ServerVariables("REQUEST_METHOD")="POST" then
\'表单提交执行
CONST upFileSize=100 \'文件大小限制,单位:KB
CONST upFileType=".gif.jpg.jpeg" \'文件类型限制,可根据需要修改
CONST targetPath="/publish/images/" \'以网站为根目录的文件存放路径,以"/"结束
dim upload,thisFile,formName,iCount
set upload=new upload_5xSoft \'\'建立上传对象
Flag=True \'初始化文件上传标识
for each formName in upload.ifile \'列出上传文件的所有参数
set thisFile=upload.ifile(formName) \'生成一个文件对象
If thisfile.filesize=0 then
Flag=False
Msg="请选择你要上传的文件。"
CALL showErr(Msg)
end if
if thisfile.filesize>upFileSize*1024 then
Flag=False
Msg="文件大小超过了限制。"
CALL showErr(Msg)
end if
if Instr(upFileType,GetExtendName(thisfile.FileName))=0 then
Flag=False
Msg="文件格式不符合要求。"
CALL showErr(Msg)
end if
If Flag then
\'创建临时上传文件夹
tempPath="/publish/images/"&Session.SessionID&"/"
set FSO= CreateObject("Scripting.FileSystemObject")
If NOT FSO.FolderExists(Server.MapPath(tempPath)) then
\'文件夹未存在则创建
FSO.CreateFolder(Server.MapPath(tempPath))
set FSO=Nothing
End if
\'执行文件上传操作
setFileName=now() \'获取当前时间作为文件名
setFileName=Replace(setFileName,"-","") \'去除日期格式符"-"
setFileName=Replace(setFileName," ","") \'去除空格
setFileName=Replace(setFileName,":","")&GetExtendName(thisfile.FileName) \'去除时间格式符":"及添加文件扩展名
thisfile.SaveAs Server.mappath(tempPath&setFileName)
End if
Next
CALL resultTip()
Response.End()
End if
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<style type="text/css">
<!--
body {
font-size: 12px;
}
-->
</style>
</head>
<body leftmargin="0" topmargin="0">
<form action="" method="post" enctype="multipart/form-data" name="frm_pic">
<input type="file" name="file">
<input type="submit" name="Submit" value="上传">
</form>
1、只能上传<%=upFileType%>格式的图片<br>
2、图片大小请在<%=upFileSize%>K以内<br>
3、请对您所上传的图片内容负责
</body>
</html>
<%
Sub resultTip()
\'本过程用于显示上传操作结果
Response.Write("<html><head>")
Response.Write("<meta http-equiv=Content-Type content=text/html; charset=gb2312>")
Response.Write("<title></title></head>")
Response.Write("<script>parent.frm_publish.P_Image.value=\'"&setFileName&"\'</script>") \'把文件名传递至父文件的隐藏表单控件
Response.Write("<body leftmargin=0 topmargin=0>")
Response.Write("<table><tr><td><font color=red><b>图片上传成功</b></font></td></tr></table>")
Response.Write("</body></html>")
End sub
Function GetExtendName(FileName)
\'本过程用于获取上传源文件的扩展名
dim ExtName
ExtName = LCase(FileName)
ExtName = mid(ExtName,instrRev(ExtName,"."))
GetExtendName = ExtName
End function
Sub showErr(msg)
\'本过程用于显示操作错误信息
Response.Write(msg)
CALL closeObject()
Response.End()
End sub
Sub closeObject()
\'本过程用于关闭对象
set upload=Nothing
set thisFile=Nothing
End sub
%>
以上源代码只要稍作修改即可。最好在父文件使用iFrame标签调用。父文件用于记录上传后文件名的隐藏表单控件名为<input name="P_Image" type="hidden" id="P_Image" value="0">,表单名为"parent.frm_publish"。如表单名和控件名改变,则相应地,源代码红色标注部分也要作相应改变,否则无法传递值。
- 上一篇: ASP 中连接 MSSQL的几种方法
- 下一篇: 解决WEB页面上
-= 资 源 教 程 =-
文 章 搜 索