用ASP做在线升级文件

七月 30, 2009 作者:FDS   类别:ASP 已经有331次浏览

用ASP做在线升级文件的方法,方便用户升级。

<%
Rem #####################################################################################
Rem ## 在线升级类声明
Class Cls_oUpdate
  Rem #################################################################
  Rem ## 描述: ASP 在线升级类
  Rem ## 版本: 1.0.0
  Rem ## 作者: 萧月痕
  Rem ## MSN:  xiaoyuehen(at)msn.com
  Rem ## 请将(at)以 @ 替换
  Rem ## 版权: 既然共享, 就无所谓版权了. 但必须限于网络传播, 不得用于传统媒体!
  Rem ## 如果您能保留这些说明信息, 本人更加感谢!
  Rem ## 如果您有更好的代码优化, 相关改进, 请记得告诉我, 非常谢谢!
  Rem #################################################################
  Public LocalVersion, LastVersion, FileType
  Public UrlVersion, UrlUpdate, UpdateLocalPath, Info
  Public UrlHistory
  Private sstrVersionList, sarrVersionList, sintLocalVersion, sstrLocalVersion
  Private sstrLogContent, sstrHistoryContent, sstrUrlUpdate, sstrUrlLocal
  Rem #################################################################
  Private Sub Class_Initialize()
   Rem ## 版本信息完整URL, 以 http:// 起头
   Rem ## 例: http://localhost/software/Version.htm
   UrlVersion     = “”
  
   Rem ## 升级URL, 以 http:// 起头, /结尾
   Rem ## 例: http://localhost/software/
   UrlUpdate     = “”
  
   Rem ## 本地更新目录, 以 / 起头, /结尾. 以 / 起头是为当前站点更新.防止写到其他目录.
   Rem ## 程序将检测目录是否存在, 不存在则自动创建
   UpdateLocalPath  = “/”
  
   Rem ## 生成的软件历史文件
   UrlHistory     = “history.htm”
  
   Rem ## 最后的提示信息
   Info        = “”
  
   Rem ## 当前版本
   LocalVersion    = “1.0.0″
  
   Rem ## 最新版本
   LastVersion    = “1.0.0″
  
   Rem ## 各版本信息文件后缀名
   FileType      = “.asp”
  End Sub
  Rem #################################################################
 
  Rem #################################################################
  Private Sub Class_Terminate()
 
  End Sub
  Rem #################################################################
  Rem ## 执行升级动作
  Rem #################################################################
  Public function doUpdate()
   doUpdate = False
  
   UrlVersion    = Trim(UrlVersion)
   UrlUpdate    = Trim(UrlUpdate)
  
   Rem ## 升级网址检测
   If (Left(UrlVersion, 7) <> “http://”) Or (Left(UrlUpdate, 7) <> “http://”) Then
    Info = “版本检测网址为空, 升级网址为空或格式错误(#1)”
    Exit function
   End If
  
   If Right(UrlUpdate, 1) <> “/” Then
    sstrUrlUpdate = UrlUpdate & “/”
   Else
    sstrUrlUpdate = UrlUpdate
   End If
  
   If Right(UpdateLocalPath, 1) <> “/” Then
    sstrUrlLocal = UpdateLocalPath & “/”
   Else
    sstrUrlLocal = UpdateLocalPath
   End If  
  
   Rem ## 当前版本信息(数字)
   sstrLocalVersion = LocalVersion
   sintLocalVersion = Replace(sstrLocalVersion, “.”, “”)
   sintLocalVersion = toNum(sintLocalVersion, 0)
  
   Rem ## 版本检测(初始化版本信息, 并进行比较)
   If IsLastVersion Then Exit function
  
   Rem ## 开始升级
   doUpdate = NowUpdate()
   LastVersion = sstrLocalVersion
  End function
  Rem #################################################################
 
  Rem ## 检测是否为最新版本
  Rem #################################################################
   Private function IsLastVersion()
    Rem ## 初始化版本信息(初始化 sarrVersionList 数组)
    If iniVersionList Then
     Rem ## 若成功, 则比较版本
     Dim i
     IsLastVersion = True
     For i = 0 to UBound(sarrVersionList)
      If sarrVersionList(i) > sintLocalVersion Then
       Rem ## 若有最新版本, 则退出循环
       IsLastVersion = False
       Info = “已经是最新版本!”
       Exit For
      End If
     Next
    Else
     Rem ## 否则返回出错信息
     IsLastVersion = True
     Info = “获取版本信息时出错!(#2)”
    End If  
   End function
  Rem #################################################################
  Rem ## 检测是否为最新版本
  Rem #################################################################
   Private function iniVersionList()
    iniVersionList = False
   
    Dim strVersion
    strVersion = getVersionList()
   
    Rem ## 若返回值为空, 则初始化失败
    If strVersion = “” Then
     Info = “出错…….”
     Exit function
    End If
   
    sstrVersionList = Replace(strVersion, ” “, “”)
    sarrVersionList = Split(sstrVersionList, vbCrLf)
   
    iniVersionList = True
   End function
  Rem #################################################################
  Rem ## 检测是否为最新版本
  Rem #################################################################
   Private function getVersionList()
    getVersionList = GetContent(UrlVersion)
   End function
  Rem #################################################################
  Rem ## 开始更新
  Rem #################################################################
   Private function NowUpdate()
    Dim i
    For i = UBound(sarrVersionList) to 0 step -1
     Call doUpdateVersion(sarrVersionList(i))
    Next
    Info = “升级完成! <a href=”“” & sstrUrlLocal & UrlHistory & “”“>查看</a>”
   End function
  Rem #################################################################
 
  Rem ## 更新版本内容
  Rem #################################################################
   Private function doUpdateVersion(strVer)
    doUpdateVersion = False
   
    Dim intVer
    intVer = toNum(Replace(strVer, “.”, “”), 0)
   
    Rem ## 若将更新的版本小于当前版本, 则退出更新
    If intVer <= sintLocalVersion Then
     Exit function
    End If
   
    Dim strFileListContent, arrFileList, strUrlUpdate  
    strUrlUpdate = sstrUrlUpdate & intVer & FileType
   
    strFileListContent = GetContent(strUrlUpdate)
   
    If strFileListContent = “” Then
     Exit function
    End If
   
    Rem ## 更新当前版本号
    sintLocalVersion = intVer
    sstrLocalVersion = strVer
   
    Dim i, arrTmp
    Rem ## 获取更新文件列表
    arrFileList = Split(strFileListContent, vbCrLf)
   
    Rem ## 更新日志
    sstrLogContent = “”
    sstrLogContent = sstrLogContent & strVer & “:” & vbCrLf
   
    Rem ## 开始更新
    For i = 0 to UBound(arrFileList)
     Rem ## 更新格式: 版本号/文件.htm|目的文件
     arrTmp = Split(arrFileList(i), “|”)
     sstrLogContent = sstrLogContent & vbTab & arrTmp(1)
     Call doUpdateFile(intVer & “/” & arrTmp(0), arrTmp(1))    
    Next
   
    Rem ## 写入日志文件
    sstrLogContent = sstrLogContent & Now() & vbCrLf
    response.Write(“<pre>” & sstrLogContent & “</pre>”)
    Call sDoCreateFile(Server.MapPath(sstrUrlLocal & “Log” & intVer & “.htm”), _
                                          “<pre>” & sstrLogContent & “</pre>”)
    Call sDoAppendFile(Server.MapPath(sstrUrlLocal & UrlHistory), “<pre>” & _
                                          strVer & “_______” & Now() & “</pre>” & vbCrLf)
   End function
  Rem #################################################################
 
  Rem ## 更新文件
  Rem #################################################################
   Private function doUpdateFile(strSourceFile, strTargetFile)
    Dim strContent
    strContent = GetContent(sstrUrlUpdate & strSourceFile)
   
    Rem ## 更新并写入日志
    If sDoCreateFile(Server.MapPath(sstrUrlLocal & strTargetFile), strContent) Then    
     sstrLogContent = sstrLogContent & “  成功” & vbCrLf
    Else
     sstrLogContent = sstrLogContent & “  失败” & vbCrLf
    End If
   End function
  Rem #################################################################
  Rem ## 远程获得内容
  Rem #################################################################
   Private function GetContent(strUrl)
    GetContent = “”
   
    Dim oXhttp, strContent
    Set oXhttp = Server.CreateObject(“Microsoft.XMLHTTP”)
    ‘On Error Resume Next
    With oXhttp
     .Open “GET”, strUrl, False, “”, “”
     .Send
     If .readystate <> 4 Then Exit function
     strContent = .Responsebody
    
     strContent = sBytesToBstr(strContent)
    End With
   
    Set oXhttp = Nothing
    If Err.Number <> 0 Then
     response.Write(Err.Description)
     Err.Clear
     Exit function
    End If
   
    GetContent = strContent
   End function
  Rem #################################################################
  Rem #################################################################
  Rem ## 编码转换 2进制 => 字符串
   Private function sBytesToBstr(vIn)
    dim objStream
    set objStream = Server.CreateObject(“adodb.stream”)
    objStream.Type    = 1
    objStream.Mode    = 3
    objStream.Open
    objStream.Write vIn
   
    objStream.Position  = 0
    objStream.Type    = 2
    objStream.Charset  = “GB2312″
    sBytesToBstr     = objStream.ReadText
    objStream.Close
    set objStream    = nothing
   End function
  Rem #################################################################
  Rem #################################################################
  Rem ## 编码转换 2进制 => 字符串
   Private function sDoCreateFile(strFileName, ByRef strContent)
    sDoCreateFile = False
    Dim strPath
    strPath = Left(strFileName, InstrRev(strFileName, “\”, -1, 1))
    Rem ## 检测路径及文件名有效性
    If Not(CreateDir(strPath)) Then Exit function
    ‘If Not(CheckFileName(strFileName)) Then Exit function
   
    ‘response.Write(strFileName)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, f
    Set fso = CreateObject(“Scripting.FileSystemObject”)
    Set f = fso.OpenTextFile(strFileName, ForWriting, True)
    f.Write strContent
    f.Close
    Set fso = nothing
    Set f = nothing
    sDoCreateFile = True
   End function
  Rem #################################################################
  Rem #################################################################
  Rem ## 编码转换 2进制 => 字符串
   Private function sDoAppendFile(strFileName, ByRef strContent)
    sDoAppendFile = False
    Dim strPath
    strPath = Left(strFileName, InstrRev(strFileName, “\”, -1, 1))
    Rem ## 检测路径及文件名有效性
    If Not(CreateDir(strPath)) Then Exit function
    ‘If Not(CheckFileName(strFileName)) Then Exit function
   
    ‘response.Write(strFileName)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fso, f
    Set fso = CreateObject(“Scripting.FileSystemObject”)
    Set f = fso.OpenTextFile(strFileName, ForAppending, True)
    f.Write strContent
    f.Close
    Set fso = nothing
    Set f = nothing
    sDoAppendFile = True
   End function
  Rem #################################################################
  Rem ## 建立目录的程序,如果有多级目录,则一级一级的创建
  Rem #################################################################
   Private function CreateDir(ByVal strLocalPath)
    Dim i, strPath, objFolder, tmpPath, tmptPath
    Dim arrPathList, intLevel
   
    ‘On Error Resume Next
    strPath     = Replace(strLocalPath, “\”, “/”)
    Set objFolder  = server.CreateObject(“Scripting.FileSystemObject”)
    arrPathList   = Split(strPath, “/”)
    intLevel     = UBound(arrPathList)
   
    For I = 0 To intLevel
     If I = 0 Then
      tmptPath = arrPathList(0) & “/”
     Else
      tmptPath = tmptPath & arrPathList(I) & “/”
     End If
     tmpPath = Left(tmptPath, Len(tmptPath) - 1)
     If Not objFolder.FolderExists(tmpPath) Then objFolder.CreateFolder tmpPath
    Next
   
    Set objFolder = Nothing
    If Err.Number <> 0 Then
     CreateDir = False
     Err.Clear
    Else
     CreateDir = True
    End If
   End function
  Rem #################################################################
  Rem ## 长整数转换
  Rem #################################################################
   Private function toNum(s, default)
    If IsNumeric(s) and s <> “” then
     toNum = CLng(s)
    Else
     toNum = default
    End If
   End function
  Rem #################################################################
End Class
Rem #####################################################################################
%>

ASP实现图片上传

七月 30, 2009 作者:FDS   类别:ASP 已经有387次浏览

用纯ASP代码来实现图片的上传以及保存到数据库的功能(顺便也实现显示数据库中的图片到网页上的功能)。
  首先我们先来熟悉一下将要使用的对象方法。我们用来获取上一个页面传递过来的数据一般是使用Request对象。同样的,我们也可以使用Request对象来获取上传上来的文件数据,使用的方法是Request.BinaryRead()。而我们要从数据库中读出来图片的数据显示到网页上面要用到的方法是:
Request.BinaryWrite()。在我们得到了图片的数据,要保存到数据库中的时候,不可以直接
使用Insert语句对数据库进行操作,而是要使用ADO的AppendChunk方法,同样的,读出数据库
中的图片数据,要使用GetChunk方法。各个方法的具体语法如下:
*Request.BinaryRead语法:
variant=Request.BinaryRead(count)
参数
variant
返回值保存着从客户端读取到数据。
count
指明要从客户端读取的数据量大小,这个值小于或者等于使用方法Request.TotalBytes得到的
数据量。
*Request.BinaryWrite语法:
Request.BinaryWritedata
参数
data
要写入到客户端浏览器中的数据包。
*Request.TotalBytes语法:
variant=Request.TotalBytes
参数
variant
返回从客户端读取到数据量的字节数。
*AppendChunk语法
将数据追加到大型文本、二进制数据Field或Parameter对象。
object.AppendChunkData
参数
objectField或Parameter对象
Data变体型,包含追加到对象中的数据。
说明
使用Field或Parameter对象的AppendChunk方法可将长二进制或字符数
  据填写到对象中。在系统内存有限的情况下,可以使用AppendChunk方法对长整型值进行
部分而非全部的操作。
*GetChunk语法
返回大型文本或二进制数据Field对象的全部或部分内容。
variable=field.GetChunk(Size)
返回值
返回变体型。
参数
Size长整型表达式,等于所要检索的字节或字符数。
说明
  使用Field对象的GetChunk方法检索其部分或全部长二进制或字符数据。在系统内存有限
的情况下,可使用GetChunk方法处理部分而非全部的长整型值。
GetChunk调用返回的数据将赋给“变量”。如果Size大于剩余的数据,则
GetChunk仅返回剩余的数据而无需用空白填充“变量”。如果字段为空,则
GetChunk方法返回Null。
  每个后续的GetChunk调用将检索从前一次GetChunk调用停止处开始的数据。但是,如果从
一个字段检索数据然后在当前记录中设置或读取另一个字段的值,ADO将认为已从第一个字段
中检索出数据。如果在第一个字段上再次调用GetChunk方法,ADO将把调用解释为新的GetChu
nk操作并从记录的起始处开始读取。如果其他Recordset对象不是首个Recordset对象的副本,
则访问其中的字段不会破坏GetChunk操作。
如果Field对象的Attributes属性中的adFldLong位设置为True,则可以对该字段使用GetChun
k方法。
如果在Field对象上使用Getchunk方法时没有当前记录,将产生错误3021(无当前记录)。
  接下来,我们就要来设计我们的数据库了,作为测试我们的数据库结构如下(access200
0):

字段名称    类型    描述
  id   自动编号   主键值
    img          OLE对象   用来保存图片数据 

对于在MSSQLServer7中,对应的结构如下:
字段名称    类型    描述
  id    int(Identity)        主键值
     img      image             用来保存图片数据 

现在开始正式编写我们的纯ASP代码上传部分了,首先,我们有一个提供给用户的上传界面
,可以让用户选择要上传的图片。代码如下
(upload.htm):
<html>
<body>
<center>
<form name=”mainForm” enctype=”multipart/form-data” action=”process.asp” method=p
ost>
  <inputtype=filename=mefile><br>
  <inputtype=submitname=okvalue=”OK”>
</form>
</center>
</body>
</html>
注意enctype=”multipart/form-data”,一定要在Form中有这个属性,否则,将无法得到上传
上来的数据。接下来,我们要在process.asp中对从浏览器中获取的数据进行必要的处理,因
为我们在process.asp中获取到的数据不仅仅包含了我们想要的上传上来的图片的数据,也包
含了其他的无用的信息,我们需要剔除冗余数据,并将处理过的图片数据保存到数据库中,这
里我们以access2000为例。具体代码如下(process.asp):
<%
response.buffer=true
formsize=request.totalbytes
formdata=request.binaryread(formsize)
bncrlf=chrB(13)&chrB(10)
divider=leftB(formdata,clng(instrb(formdata,bncrlf))-1)
datastart=instrb(formdata,bncrlf&bncrlf)+4
dataend=instrb(datastart+1,formdata,divider)-datastart
mydata=midb(formdata,datastart,dataend)
setconnGraph=server.CreateObject(“ADODB.connection”)
connGraph.ConnectionString=”driver={MicrosoftAccessDriver(*.mdb)};DBQ=”&server.Ma
pPath(“images.mdb”)&”;uid=;PWD=;”
connGraph.Open
setrec=server.createobject(“ADODB.recordset”)
rec.Open”SELECT*FROM[images]whereidisnull”,connGraph,1,3
rec.addnew
rec(“img”).appendchunkmydata
rec.update
rec.close
setrec=nothing
setconnGraph=nothing
%>
好了,这下我们就把上传来的图片保存到了名为images.mdb的数据库中了,剩下的工作就是要
将数据库中的图片数据显示到网页上面了。一般在HTML中,显示图片都是使用<IMG>标签
,也就是<IMGSRC=”图片路径”>,但是我们的图片是保存到了数据库中,“图片路径”是什么
呢?呵呵,其实这个SRC属性除了指定路径外,也可以这样使用哦:
<IMGSRC=”showimg.asp?id=xxx”>
所以,我们所要做的就是在showimg.asp中从数据库中读出来符合条件的
数据,并返回到SRC属性中就可以了,具体代码如下(showimg.asp):
<%
setconnGraph=server.CreateObject(“ADODB.connection”)
connGraph.ConnectionString=”driver={MicrosoftAccessDriver(*.mdb)};DBQ=”&
server.MapPath(“images.mdb”)&”;uid=;PWD=;”
connGraph.Open
setrec=server.createobject(“ADODB.recordset”)
strsql=”selectimgfromimageswhereid=”&trim(request(“id”))
rec.openstrsql,connGraph,1,1
Response.ContentType=”image/*”
Response.BinaryWriterec(“img”).getChunk(7500000)
rec.close
setrec=nothing
setconnGraph=nothing
%>
注意在输出到浏览器之前一定要指定Response.ContentType=”image/*”,
以便正常显示图片。
最后要注意的地方是,我的process.asp中作的处理没有考虑到第一页(upload.htm)中还有其
他数据,比如<INPUT type=tesxt name=userid>等等,如果有这些项目,你的process.asp就
要注意处理掉不必要的数据。

php访问查询mysql数据的三种方法

七月 30, 2009 作者:FDS   类别:PHP 已经有334次浏览

1. $row = mysql_fetch_row($result);
返回一个规则的数组$row,$row[0]是第一个元素,$row[1]是第二个元素,依次类推…
mysql_num_fields($result) 返回结果的元素个数。

2. $row = mysql_fetch_array($result);

返回一个数组$row. 举例如下:
表结构如下:

username | password
————————————-
bourbon | abc
berber | efg

第一次运行运行 $row = mysql_fetch_array($result) 则结果如下:

$row[0] = $row["username"] = “bourbon”
$row[1] = $row["password"] = “abc”

第一次运行运行 $row = mysql_fetch_array($result) 则结果如下:

$row[0] = $row["username"] = “berber”
$row[1] = $row["password"] = “efg”

3. $row = mysql_fetch_object($result);

返回一个对象描述行. 如上例
第一次运行运行$row = mysql_fetch_object($result) 则结果如下:

$row->username = “bourbon”
$row->password = “abc”

第二次运行运行$row = mysql_fetch_object($result) 则结果如下:
$row->username = “berber”
$row->password = “efg”

在Asp程序中取得表单所有内容的方法

七月 30, 2009 作者:FDS   类别:ASP 已经有266次浏览

在Asp中如何得到所有表单的名称跟对应的值。其实,这个问题很简单,但是可能还是有很多人不知道该怎么做,所以特地写下来,仅供参考。在Asp程序中,用来获得客户端数据的对象是 Request,这个对象给我们提供了很多的方法以及属性。比如,有这样一个Form,

<FORM METHOD=POST name=cqq ACTION=”">
 <INPUT TYPE=”text” NAME=”username”>
 <INPUT TYPE=”text” NAME=”password”>
 <INPUT TYPE=”checkbox” NAME=”sex” value=”male”>
 <INPUT TYPE=”checkbox” NAME=”sex” value=”female”>
 <INPUT TYPE=”submit”>
 </FORM>

         如果我们要取得 username 中的值,我们可以这样写:Request.Form(“username”)

 这个大家都会,其实这个Form是一个集合,也就是说表单中的所有的内容都存放在这个集合

当中,我们要取得某个元素的值,只需要在Request.Form() 这里制定元素的名称就可以了,比如

上面的username。

          那么,我们要取得集合中所有的值呢?  那很简单,什么都不用跟就是了,直接写Request.Form

就得到了集合中所有元素的名称跟值。  下面是一个对集合操作的语句:

<%
For each obj in Request.Form
     Response.write obj & ” ” & Request.Form(obj) &  ” <br>”
Next
%>