So far, except for the file we just uploaded, any information we've been storing has been in the database. Now we're going to look at actually writing to the hard drive on the server.
The task at hand is to take the raw file that we uploaded in the last section and use it to create a formatted news file. The best way to do this—and the way requiring the least hassle for maintenance—would be to create a file that has our header and footer and a Server Side Include for the raw file in the middle.
To do this, we're going to use the fileSystemObject object. Yes, that's really what it's called. The fileSystemObject is part of the Scripting object we saw when we were creating a dictionary. The fileSystemObject has everything we need to manipulate, read, and write files, directories, and pretty much anything else we need to do on the disk.
That's lot of complexity, though, so let's start small. All we want to do is create a text file, write to it, and close it.
Add the code in Listing 8.16 to edit_news_rec_action.asp.
On line 18, we're recording the complete path of the first file uploaded, including the drive and directory information. It's also the only file we uploaded, but we still need to specify a number. On line 19, we're stripping out the path information by replacing it with an empty string, so that all we are left with is the name.
On line 22, we're actually creating the fileSystemObject. Even though we're not using it directly, we still need to create it so we can create other things, such as the new file, from it.
After we've got the fileSystemObject, on line 23 we're using it to create a new file. We've specified the path, including the name that we pulled from the form. This gives us the location where the browser will be expecting to find the file.
Note
Remember that c:inetpubwwwroot is our home directory. If you've got your home directory somewhere else, make the changes accordingly.
After we've created the file, it's a simple matter of writing to it, which we do on lines 28 through 33. All we really need are three lines: the top, the content, and the bottom. So for a new filename of fanfic.asp and an old filename of fannominations.txt, the new file would be at
C:inetpubwwwrootfanfic.asp
and would read like this:
<!--#include file="pagetop.txt"--> <!--#include file="fannominations.txt" --> <!--#include file="pagebottom.txt"-->
After we've written those lines, we can close the text file on line 34, and then close and destroy the fileSystemObject objects and the Upload object.
This is great, but what if all the administrator wants to do is change the database information and not re-upload the file? We wouldn't want to replace a perfectly good file with an empty one. To get around this, we can check to see whether there was a file uploaded and only write out the new news file if there was. Add the changes in Listing 8.17 to edit_news_rec_action.asp.
Whether we collect it, as we're doing on line 9, Upload.Save will return the number of files that were uploaded. This way, we can check and create the new file or not accordingly.
Before we move on to reading files, we do have two loose ends to clean up with regards to the news. We can add a file, and we can change and delete the database entries for the file, but we haven't considered how to remove or change the names of the files that we have on the filesystem already.
GEEK SPEAK: The filesystem is made up of the local and remote disk drives attached to a computer.
Let's take a look at the logic first, and then we'll worry about how to do it. Add the code in Listing 8.18 to edit_news_rec_action.asp.
There is only one time when this is going to matter: when we're editing an existing record, and we've changed the name of the final file. Otherwise, there's no old file to change or delete.
So let's say that we are updating an existing record, and we've changed the name. This leads us to another fork in the road. Either we've uploaded a new file, in which case we need to delete the old one, or we haven't, in which case we just want to change the name on the old one. Add the changes in Listing 8.19 to edit_news_rec_action.asp.
… 48: if p_existing = "" then 49: insertSet.AddNew 50: else 51: insertSet.Find "news_id =" & p_existing 52: if insertSet("news_url") <> p_news_url then 53: 54: p_oldFilePath = "c:inetpubwwwroot"& insertSet("news_url") 55: p_newFilePath = "c:inetpubwwwroot"& p_news_url 56: 57: set oldFileObject = Server.CreateObject("Scripting.FileSystemObject") 58: if oldFileObject.fileExists(p_oldFilePath) then 59: if fileUploaded then 60: oldFileObject.DeleteFile(p_oldFilePath) 61: else 62: oldFileObject.MoveFile p_oldFilePath, p_newFilePath 63: end if 64: end if 65: set oldFileObject = Nothing 66: end if 67: end if … |
So if we are going to have to do anything, we're going to need to know what we're moving (or deleting) and to where (if anywhere). On lines 54 and 55, we're setting the old and new path information, from the database and the form respectively. On line 57, we're creating the actual fileSystemObject that's going to do the work.
Before we try and do anything, we need to account for the possibility that the old file has already been cleaned up, so on line 58, we'll check to see if it still exists. If it doesn't, we won't do anything.
If it does exist, we need to handle it. If it's already been replaced, we need to delete it, and we'll do that on line 60. If we're just renaming, we'll do that on line 62 by "moving" the file. Even though they're both in the same place, it's still referred to as moving the file, only it's from one name to the other instead of one directory to the other.
That takes care of our loose ends in edit_news_rec_action.asp. No matter what happens, our file should be right. Now we just have to delete a file when we remove the reference to it in edit_news_action.asp (see Listing 8.20).
This is exactly the same code we used in the previous example, except that we've changed insertSet to deleteSet and we don't need to worry about renaming the file.
So we can now add, update, and delete information not only in the database but on the filesystem itself.
18.217.186.244