![]() | Pepper Spray & Stun Gun Specials! KEEP THEM SAFE. | |
| CoverYourASP --> Displaying the contents of files on the server | ||
| Quite often I need to show you the contents of a file on my server. Obviously being a ASP source site I may need to do this more than most sites, but it's a useful tutorial nonetheless. Firstly, it demonstrates the use of the FileSystemObject, and secondly it shows how powerful regular expressions can be. I learnt all I know about regular expressions from Microsoft's online documentation and a lot of experimentation! I created a function, ShowFile( ), which I keep in the utils/ShowFile.asp Server Side Include. Let's look at that function:
I pass in a FileSystemObject (oFSO, you'll see how I call it in a minute), a filename (sFile), and whether to display any embedded HTML or allow the browser to act upon it (bPassHTML). The next parameter, bShowName, defines whether the filename is displayed, and bLiveLinks specifies if it should convert any http://... strings into live hyperlinks. The first thing I do is to open the file and read in the entire contents of the file into the variable s. I then close the file. This is important! Remember to always close as soon as possible to conserve precious server resources. This applies to files, objects, database connections - anything and everything you create or open...
Now comes the fun part. If the HTML is to be displayed to you, i.e. you want to see <b>hello</b>, not just hello, I find and replace certain characters in the buffer. The order that you do these replacements is important. The latest one I added recently was to make any hyperlinks in the buffer "live" - very cool when displaying my newsletter archive that contains many "http:...".
Next, a <font> tag is wrapped around the buffer to display all the text in black, and the filename added to the top if needed.
All that's left then is to output the buffer to your browser. Below is an example of how to call the function - see how the FilesystemObject is opened once, and the object passed into ShowFile( ). This is so that you can create the object once and use it to display many files.
Remember, you can download all of my source code, including this handy SSI and many others! |
| |||||||||||||||||||||||||||||||||||||||||||||||||||||