Conceptual Explanation

In this chapter, we will address reading and writing of text files, using a file holding the names and scores of players, and uploading of files, such as image files used to show products.

Files and Records

Text files hold lines of text. Using the terminology of files, a record in the text file corresponds to a line when the file is viewed in any text editor program, such as Notepad in Windows.

Because each file has its own structure, the middleware programs must be able to operate at the file, line, or character level. The PHP system provides functions for reading the entire file at once, reading one line at a time, or reading one character at a time. Moreover, PHP provides a facility to read and print out the entire file with one instruction. The ASP system provides methods to the TextStream object for reading the entire file, reading a line at a time, or reading a certain number of characters. Each system, by functions or methods, provides ways to skip characters or lines to get to a specified point in the file.

The internal structure of a line does not matter to the file handling functions in the scripts. In the example described in this chapter, each line (record) of the file will be a name followed by a comma and then a number in character form. This represents the name of a player and a score. To extract information from a record, your code makes use of the comma. Both PHP and ASP have facilities that generate arrays from strings based on specification of a delimiter. In PHP, the explode function takes as arguments a string representing the delimiter and an original string and produces an array.

    $items = explode(",",$original);

In ASP, the split method of string objects does the same job:

    items = original.split(",");

If $original or original consisted of:

    Tom, 98

then $items and items would be arrays with two elements:

    "Tom"      "98"   

Reading and Writing Text Files with PHP and ASP

The examples in this chapter show how to read and write to a text file. More specifically, the code reads in all the existing records, displays them, and then allows the person at the client computer to enter new information. The new information is appended (added) as a new record to the file. It is generally necessary to specify how a file is to be used by indicating what is called the mode of use. Although different systems define mode in different ways, the general categories are:

  • Read

  • Write data to the file from the beginning, erasing any existing information

  • Append (add) data to the end of what is already in the file

Establishing the mode of access is done as part of what is called the open operation. The act of opening a file is analogous to establishing the connection in databases. The system sets up links and reserves buffer space for the subsequent file operations. As with databases, your code should release these system resources by issuing a call to close the file, after it is done accessing it.

Although as you would expect by this time, the coding for the PHP and ASP systems appears different for handling files but supports similar functionality. The ASP system uses methods of objects: the Scripting.FileSystemObject and a file stream object created by use of a method of the file system object. The Scripting object is a Windows object as opposed to an ASP object. This is of no practical significance, except that you might come across this when using other Windows tools. The methods used in this chapter are ReadLine, write, FileExists, OpenTextFile, AtEndofStream,and close. The ASP methods and properties for file handling, including some not used in this chapter, are shown in Table 12.1.

Table 12.1: ASP Methods and Properties for File Handling


Property holding condition of being at the end or not


Closes the connection to the file


Checks if a file as specified by the path fp exists


Establishes a connection to the specified file for mode access, with options cop and fop


Read nc characters


Read the entire file (similar to PHP’s file)


Reads in one line from file stream


Writes the string s to the file


Writes out the string s and adds an end-of-line character


Skip nc characters


Skip nl lines

The PHP used in this chapter are the simple functions fopen, file (for reading in the whole file), fwrite, and fclose. The PHP functions for file handling are shown in Table 12.2.

Table 12.2: PHP Functions for Files


Reads in the file at $fp


Establishes connection with file for mode access. Returns value to be used by later commands

fread($fp, nc)

Read in nc characters

fgets($fp, nc)

Read in until reach end-of-line character or nc characters or until the end-of-the-file, which ever comes first


Writes the string $s to the file opened


Goes (seeks) to the point nc characters into the file

flock($fp, type)

Controls if or how the file can be used by others. A setting of type=1 allows other readers; type = 2 prevents shared use; a setting of 3 releases an existing lock


Closes connection to the file

To demonstrate to you that the systems can work together, both the ASP and PHP score-keeping application uses the same file. That is, you can invoke the ASP code to add a record to the file, and then, when you invoke the PHP code, you will see the file with the new information added. You also can do the reverse: add a record using the PHP script and see it using the ASP script. Independently of ASP or PHP, you can examine the file. Since the suggested file extension is “txt” and not ASP or PHP, you can view the file using a browser or download it. This should make sense to you: the file does not retain any connection with the ASP or PHP programs.

The single file used for both systems is located in the same folder as the ASP and PHP scripts. Each script will calculate the location of the file using functions and methods, respectively, for determining the address of the script itself. The calculation will be done using regular expressions to remove the part of the URL that points to the script and replace it with the name and extension of the file.

Since file handling might involve things that you cannot tightly control, you will need to think about detecting errors. For example, what if someone has removed the file from the server computer? Your code must take the correct action and not display a cryptic error for your customers. The PHP script uses the @ operator to prevent an error on opening the file, and then checks if a file was opened. The ASP script uses the FileExists method.

Uploading a File Using PHP

The task described here and in a later chapter is that of uploading an image file that shows a product for the online store. An ordinary customer would not perform this task, but employees of the store would. You, as the system designer and builder, might choose to implement adding new product information as part of the Web application, and this could involve the uploading of images of the product.

The uploading file task is more problematical than you might suppose. This is because uploading an unknown file to a server is a risky operation for the operators of the server. The upload operation requires modifying the directory of the server. Letting a program change the directory provides an opening for a hacker to do considerable damage. In addition, the uploaded file might prove to be a problem. What if the uploaded file was a substitute for an existing file used by the system or by another user? The PHP system does provide functions for uploading files, and tools are available for ASP. However, you will need to check with your server administration to see if you can incorporate such code in your application. This chapter will show the PHP solution.

Programming the uploading operation as performed in the PHP system consists of several subtasks. You must code a form suitable for uploading files. The form in which the person sitting at the client computer specifies the file to be uploaded requires the:


attribute in the form tag. The input tag is of type “file.”

In the part of the script that handles the form input, your code must copy the file to its proper location. This means that you need to calculate an address similar to what is required for reading and writing files.

Creating Database Web Applications with PHP and ASP
Creating Database Web Applications with PHP and ASP (Charles River Media Internet & Web Design)
ISBN: 1584502649
EAN: 2147483647
Year: 2005
Pages: 125
Authors: Jeanine Meyer

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: