Display a Random Image

This function is very useful when you need to randomly display a series of graphics. Simple save them into a folder and apply this function. Please note this function accepts virtual directory as argument, you can modify it to accept absolute harddrive directory.

 Function RandomImage(strPath)
    If Right(strPath, 1) <> Chr(47) Then strPath = strPath & Chr(47)
     strPhysical = Server.MapPath(strPath)
     Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
     Set objFolder = objFSO.GetFolder(strPhysical)
     Set objFiles = objFolder.Files
    For Each objFile in objFiles
       strFile = LCase(objFile.Name)
       strFiles = strFiles & strFile & vbTab
    strImages = Split(strFiles, vbTab)
     If UBound(strImages) > 1 Then
       RandomImage = strPath & strImages(Int(Rnd(1)*UBound(strImages)))
     End If
 End Function

<img src="<%=RandomImage("/images")%>" border=1>

The Result:

random image asp Server.MapPath graphics FileSystemObject fso

Back To Top
© 1998 - 2017 psacake.com | My3C's

Version 7.2 | Advertise on this site