Creating Files on the Server: Formatting the News

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.

Code Listing 8.16. edit_news_rec_action.asp: Writing a text file
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <%  Response.buffer = true  %>
2: <!--#include file="adovbs.inc"-->
3: <!--#include file="pagetop.txt"-->
4: <!--#include file="isadmin.inc"-->
5: <%
6: if isAdmin then
7:
8:     set Upload = Server.CreateObject("PersitsUpload.1")
9:     Upload.Save("c:inetpubwwwroot")
10:
11:    p_news_id = cint(Upload.form("p_news_id"))
12:    p_news_cat_id = cint(Upload.form("p_news_cat_id"))
13:    p_news_headline = cstr(Upload.form("p_headline"))
14:    p_news_blurb = cstr(Upload.form("p_blurb"))
15:    p_news_url = cstr(Upload.form("p_url"))
16:    p_existing = cstr(Upload.form("p_existing"))
17:
18:    p_subfile_name = Upload.Files(1).Path
19:    p_subfile_name = replace(p_subfile_name, _
20:                                   "c:inetpubwwwroot", "")
21:
22:    set newFileObject = _
23:             Server.CreateObject("Scripting.FileSystemObject")
24:    set newFile = _
25:              newFileObject.CreateTextFile("c:inetpubwwwroot" _
26:                                                      & p_news_url)
27:
28:    newFile.WriteLine("<!--#include file=" &chr(34)&_
29:                                  "pagetop.txt" &chr(34)&"-->")
30:    newFile.WriteLine("<!--#include file=" &chr(34)&_
31:                                 p_subfile_name &chr(34)&"-->")
32:    newFile.WriteLine("<!--#include file=" &chr(34)&_
33:                               "pagebottom.txt" &chr(34)&"-->")
34:    newFile.Close
35:
36:    set newFile = Nothing
37:    set newFileObject = Nothing
001 38:
002 39:    set Upload = Nothing
003 40:
004 41:    set outpostDB = Server.CreateObject("ADODB.Connection")
005 42:    outpostDB.Open "outpost"
006 43:
007 44:    set insertSet = Server.CreateObject("ADODB.RecordSet")

45:    insertSet.Open "news", outpostdb, adOpenDynamic, _
46:         adLockOptimistic, adCmdTable
47:
48:    if p_existing = "" then
49:        insertSet.AddNew
50:    else
51:        insertSet.Find "news_id ="& p_existing
52:    end if
53:    insertSet("news_id") = p_news_id
54:    insertSet("news_cat_id") = p_news_cat_id
55:    insertSet("news_headline") = p_news_headline
56:    insertSet("news_blurb") = p_news_blurb
57:    insertSet("news_url") = p_news_url
58:    insertSetUpdate
59:    insertSet.Close
60:    set insertSet = Nothing
61:
62:    outpostDB.Close
63:    set outpostDB = Nothing
64:
65:    Response.Clear
66:    Response.Redirect "http://localhost/edit_news.asp"
67:else
68:
69:    Response.Write "You do not have access to this page."
70:
71:end if
72:%>
73:</BODY>
74:</HTML>

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.

Code Listing 8.17. edit_news_rec_action.asp: Checking for an uploaded file
…
8:   set Upload = Server.CreateObject("PersitsUpload.1")
9:   p_count = Upload.Save("c:inetpubwwwroot")
10:
11:  p_news_id = cint(Upload.form("p_news_id"))
12:  p_news_cat_id = cint(Upload.form("p_news_cat_id"))

13:  p_news_headline = cstr(Upload.form("p_headline"))
14:  p_news_blurb = cstr(Upload.form("p_blurb"))
15:  p_news_url = cstr(Upload.form("p_url"))
16:  p_existing = cstr(Upload.form("p_existing"))
17:
18:  if p_count = 1 then
19:      p_subfile_name = Upload.Files(1).Path
20:      p_subfile_name = replace(p_subfile_name, "c:inetpubwwwroot", "")
21:      fileUploaded = true
22:  else
23:      fileUploaded = false
24:  end if
25:
26:  if fileUploaded then
27:      set newFileObject = _
28:           Server.CreateObject("Scripting.FileSystemObject")
29:      set newFile = _
30:            newFileObject.CreateTextFile("c:inetpubwwwroot" _
31:                                                    & p_news_url)
32:
33:      newFile.WriteLine("<!--#include file=" &chr(34)&_
34:                                "pagetop.txt" &chr(34)&"-->")
35:      newFile.WriteLine("<!--#include file=" &chr(34)&_
36:                               p_subfile_name &chr(34)&"-->")
37:      newFile.WriteLine("<!--#include file=" &chr(34)&_
38:                             "pagebottom.txt" &chr(34)&"-->")
39:      newFile.Close
40:
41:      set newFile = Nothing
42:      set newFileObject = Nothing
43:  end if
44:
45:  set Upload = Nothing
46:
…

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.

Code Listing 8.18. edit_news_rec_action.asp: Preparing to delete or rename files
…
44:  set insertSet = Server.CreateObject("ADODB.RecordSet")
45:  insertSet.Open "news", outpostdb, adOpenDynamic, _
46:      adLockOptimistic, adCmdTable
47:
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:          if fileUploaded then
54:              'DELETE OLD FILE
55:          else
55:              'RENAME OLD FILE
57:          end if
58:      end if
59:  end if
60:  insertSet("news_id") = p_news_id
61:  insertSet("news_cat_id") = p_news_cat_id
62:  insertSet("news_headline") = p_news_headline
63:  insertSet("news_blurb") = p_news_blurb
64:  insertSet("news_url") = p_news_url
65:  insertSetUpdate
66:  insertSet.Close
67:  set insertSet = Nothing
…

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.

Code Listing 8.19. edit_news_rec_action.asp: Deleting or changing files
…
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).

Code Listing 8.20. edit_news_action.asp: Deleting a file that's no longer in use
0: <%@ LANGUAGE="VBSCRIPT" %>
1: <%  Response.buffer = true  %>
2: <!--#include file="adovbs.inc"-->
3: <!--#include file="pagetop.txt"-->
4: <!--include file="isadmin.inc"-->
5: <%
6: if isAdmin then
7:
8:     set outpostDB = Server.CreateObject("ADODB.Connection")
9:     outpostDB.Open "outpost"
10:
11:    set deleteSet = Server.CreateObject("ADODB.RecordSet")
12:    deleteSet.Open "select * from news order by news_id", _
13:        outpostDB, adOpenDynamic, adLockPessimistic, adCmdText
14:    p_thisDelete = 1
15:    while not deleteSet.EOF
16:        if cint(deleteSet("news_id")) = _
17:          cint(Request.form("p_delete").item(p_thisDelete)) then
18:
19:            set oldFileObject = _
20:              Server.CreateObject("Scripting.FileSystemObject")
21:            p_oldFilePath = _
22:              "c:inetpubwwwroot"& deleteSet("news_url")
23:            if oldFileObject.fileExists(p_oldFilePath) then
24:                oldFileObject.DeleteFile(p_oldFilePath)
25:            end if
26:            set oldFileObject = Nothing
001 27:
002 28:            deleteSet.Delete
003 29:            deleteSet.Update
004 30:            if p_thisDelete < Request.form("p_delete").Count then

31:                p_thisDelete = p_thisDelete + 1
32:            end if
33:        end if
34:        deleteSet.MoveNext
35:     wend
36:     deleteSet.Close
37:     set deleteSet = Nothing
38:
39:     outpostDB.Close
40:     set outpostDB = Nothing
41:
42:     Response..Clear
43:     Response.Redirect "http://localhost/edit_news.asp"
44:else
45:
46:     Response.Write "You do not have access to this page."
47:
48:end if
49:%>
50:</BODY>
51:</HTML>

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.186.244