Random ASP – Dynamic File Includes
Below is a function I use occasionally for dynamically including a text file into an ASP page, particularly when using query strings to select the file. Server-side Includes (SSI) are executed before ASP. This means that even if you code the query string in-line with the SSI, the text will not show up.
<%
Function IncludeText(strFilePath)
Dim fs, fname
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fname = fs.OpenTextFile(Server.MapPath(strFilePath),1)
fname.ReadAll
fname.Close
fs.Close
fname = Nothing
fs = Nothing
End Function
%>
Simple! We’re just creating a file system object, opening the requested file, and reading all its contents to the browser. Since we made this a function, we can call it with a simple line of code.
<%
Call IncludeText(Request.QueryString("pathto/filename.txt"))
%>
If you’re not going to use query strings and just go with a standard file every time, this is probably overly done and you can just use a server-side include.
<!--#include file="./pathto/filename.txt"-->
