ASP Open/Read Text File

Access your web server's file system

One method I find myself using over and over again is opening and reading text files using the built in FileSystemObject object. Here are my notes on getting this object up and running in no time.

NOTE: The first thing you need to do is make sure that the IUSR account has modify/write permissions on the folder and/or file(s) that you are going to access. What I usually do is to create a special folder just for accessing files and only give that folder permission. This will limit the amount of vulnerable directories that you have open.

Below is the code that I use to create the FileSystemObject object, check if the file exists and read the contents of the file.

<%

Dim objFileSystemToRead,FileToRead
Dim strFileName

strFileName = "fileToRead.txt"

Set objFileSystemToRead = Createobject("scripting.filesystemobject")
If objFileSystemToRead.FileExists (server.mappath("/folder/" & strFileName)) Then

     Set objFileSystemToRead = Createobject("scripting.filesystemobject")
     Set FileToRead = objFileSystemToRead.opentextfile(Server.MapPath("/folder/" & strFileName),1,False,0)

     Do While Not FileToRead.AtEndOfStream

          Response.Write(FileToRead.readline)

     Loop

Else
     Response.Write("File Does Not Exist")
End IF

%>

ASP

Ads