This article describes how to build a very powerful, potentially destructive application using the File System Object. It should never be implemented in a production environment without ensuring that proper security precautions have been taken to maintain the integrity of the data contained on the web server.
Overview
I built the ASP File Manager so that I could quickly gain access to web applications that I maintain on remote servers. It is a strictly web-based administration system that allows me to create, edit and delete text-based files, manage folders, and query and update databases. There are several other pieces that I use to check for installed components on the server and view all of the collection. s contents.
When I started building the scripts, I knew that I wouldn. t be satisfied if the finished application didn. t meet the following requirements:
¨ Allows me to create new folders and delete existing folders and their contents ¨ Allows me to create new text-based documents and edit and delete existing text documents ¨ Verifies the delete or overwrite of all files and folders ¨ Allows me to view and edit the information contained in each table of any given database (using either connection strings or file DSNs to connect to the database, Access or SQL) ¨ Provides a familiar way to . wade through. the directory structure on the remote server . clickable folders and files.
Assumptions
The following scripts makes several assumptions regarding its location on the server and the files contained in that location and available to it. To properly configure your directory so that the scripts function properly, follow these instructions:
¨ place all scripts and related files mentioned below in a directory off the root of the webserver (I call mine toolbox) ¨ make sure that ADOVBS.INC is accessible from the root of the webserver.
With these details taken care of, let. s get started on the backbone of the system: The File Manager itself.
Subroutines
The subroutines below are the guts of the file manager. Each one encapsulates the code necessary to open a text file or to create a folder. They should all be placed in a single file that will be included in our runtime script (I call mine fileman_inc.asp).
Edit Database The edit database subroutine contains the code that allows you to click through on an Access or SQL database file and edit it using a third-party application. It returns an error unless the file named in the runtime script as the database gateway file exists in the same directory as the file manager script. (This file will be created in another article).
Sub EditDb If fs.FileExists(server.mappath(dbfile)) Then Response.Redirect dbfile & "?db=" & sFile Else Response.Write "The database access functionality is not installed on this server." &vbCrLf End If End Sub
Sub EditFile The edit file subroutine contains the code that allows you to click through on a text-based file and edit its contents.
Sub EditFile Session("lastpage") = Request.ServerVariables("HTTP_REFERER") Set ReadStream = fs.OpenTextFile(server.mappath(sFile))
Sub CreateFile The create file subroutine contains the code that allows you to create a new text-based file in a given directory within the webserver.
Sub CreateFile Session("lastpage") = Request.ServerVariables("HTTP_REFERER") Response.Write "<p><a href=""" & Session("lastpage") & """>..back...</a></p>" &vbCrLf Response.Write "<form method=""POST"" action= """ & scriptname&"?action=savenewfile&path=" & sPath & """>" &vbCrLf Response.Write "<p><input type=""text"" name=""file""></input> type new file name here (include extension)</p>" &vbCrLf Response.Write "<p>type new file text here<br><textarea rows=""20"" cols=""70"" name=""newfilestuff""></textarea></p>" &vbCrLf Response.Write "<p><input type=""submit"" value=""Create File"" name=""submit""></p>" &vbCrLf Response.Write "</form>" &vbCrLf End Sub
Sub SaveFile The save file subroutine saves files created or edited using this script. If it is a new file, it first checks to make sure that a file doesn. t exist in that location with the same name. If one does, it prompts you to make sure that you want to overwrite the existing file.
Sub SaveFile If Request.Querystring("overwrite") = "yes" Then Set WriteFile = fs.CreateTextFile(server.mappath(Session("sFile")), true) WriteFile.Write Session("newfilestuff") WriteFile.Close Response.Redirect("" & Session("lastpage") & "") Else Session("lastpage") = Request.ServerVariables("HTTP_Referer") If fs.FileExists(server.mappath(sFile)) Then Session("sFile") = sFile Session("newfilestuff") = Request.Form("newfilestuff") Response.Write "<p>A file called <b>" & sFile & "</b> already exists!</p>" Response.Write "<UL>" Response.Write "<LI><a href= """ & scriptname&"?action=savenewfile&overwrite=yes"">Overwrite Existing File</a>" ' We don't want to lose the information that the typed in the previous form if they decide NOT to overwrite the existing file, so we provide a javascript link back that works exactly the same as the browser's back button. Response.Write "<LI><a href=""javascript:history.back()"">Return to previous page</a></LI>" Response.Write "</UL>" Else Set WriteFile = fs.CreateTextFile(server.mappath(sFile), false) WriteFile.Write Request.Form("newfilestuff") WriteFile.Close Response.Redirect("" & Session("lastpage") & "") End I
f End If End Sub
Sub CreateFolder The create folder subroutine contains the code that allows you to create a new folder in a given directory within the webserver.
Sub CreateFolder Session("lastpage") = Request.ServerVariables("HTTP_REFERER") If fs.FolderExists(server.mappath(sFolder)) Then response.write "A folder called <b>" & sFolder & "</b> already exists!<br>" Else fs.CreateFolder(server.mappath(sFolder)) Response.Redirect("" & Session("lastpage") & "") End If End Sub
The Author Misty Myslinski is a
hired gun (read: contractor) saving corporate america from the tedium of
menial tasks. She thinks that anything and everything should be done over
the corporate intranet and is currently working to realize her vision. She
also does freelance web-application development in her free minutes.