
- •Initializing with Constructor Functions . . . . .
- •Into a Web page as a separate section. Although JavaScript code can
- •Is that standard php script delimiters are guaranteed to be available
- •In the block. Any text or lines between the opening /* characters and
- •2.7541 Are not integers; they are floating-point numbers. A floating-
- •Value 300
- •Is a value of 2.5, because 6 goes into 15 exactly 2.5 times. But if you
- •IsEven.Php.
- •Ing example,
- •Ing curly brace is on its own line following the function statements.
- •In php 3 and earlier, it was necessary to put a function definition
- •Is called an iteration. When the conditional expression evaluates
- •Including Files
- •13. Close your Web browser window.
- •Including Files
- •In php, you can also use two operators to combine strings. The first
- •Xhtml source code gen-
- •Input. Php provides several functions for manipulating the case of a
- •Is uppercase. If you need the reverse of ucfirst(), the lcfirst()
- •In some situations, you will need to find and extract characters and
- •Information Interchange, or ascii, which are numeric represen-
- •In comparison, the following preg_match() function returns a value
- •In the pattern is optional. The following code demonstrates how to
- •Values; any strings you validate against a regular expression must
- •Value of 1 because the top-level domain contains a valid value of .Com.
- •Is submitted using the “post” method, the form data is embedded in
- •Validating String Data
- •Xhtml tags or character entities. The message field is a text string
- •Value of the header element. For example:
- •Xhtml code within a php script section.
- •Is typically the person who created the resource. Otherwise, the net-
- •If even a single character of the Web page is sent prior to sending
- •Variables to the file_put_contents() function.
- •Xhtml hyperlink. To download a file from outside the xhtml
- •If...Else statement to display the appropriate version of the mes-
- •Iterating Through an Array
- •Iterating Through an Array
- •In Chapter 2, you learned how to use a foreach statement to iterate
- •Iterating Through an Array
- •Iterating Through an Array
- •In comparison, the following code declares and initializes
- •If ((!file_exists("MessageBoard/messages.Txt"))
- •Values from the array to create a thumbnail gallery of images in which
- •Introduction to Databases
- •Including php, allow you to create Web pages that can read and write
- •Introduction to Databases
- •Information that can be organized into ordered sets of data, and
- •Information. Each recipe in a recipe database, for instance, is a single
- •Introduction to Databases
- •Index, which identifies records in a database to make retrievals and
- •In a single table. However, you might want to break the information
- •Into multiple tables to better organize it into logical sets. Another
- •Information in one of the tables confidential and accessible only by
- •Is the employee information table from Figure 7-1. The related table
- •Is a payroll table that contains confidential salary and compensation
- •Information. Notice that each table contains an identical number of
- •Introduction to Databases
- •Introduction to Databases
- •In a junction
- •Introduction to Databases
- •In a relational format is called a relational database management
- •Is a standard data manipulation language among many dbmSs.
- •Into the query area at the top of the screen or by dragging tables and
- •It is important to understand that even though many dbmSs sup-
- •Introduction to Databases
- •If you ever
- •Is. In comparison, the bigint data type stores integer values between
- •5 Rows in set (0.00 sec)
- •Int);[enter ]
- •Important, these two tabs can cause you to lose all of the data in the
- •Internet Explorer to export the table, click the Save button in the File
- •Ifies the table being changed and the change to make.
- •It easier for you to write php code that can be used with a variety of
- •Information about queries that match one of the following formats:
- •Various types of actions, depending on the type of query.
- •Include fields for the date and time of the flight, flight number, and
- •In the ChineseZodiac folder and upload the file to the server. Open
- •Including white space,
- •Information on a Web server. When you start a new session, the
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented
- •Variables associated with an object are called properties or attributes.
- •In the Loan object example, a function that calculates the number of
- •Introduction to Object-Oriented Programming
- •Introduction to Object-Oriented Programming
- •Include instances of objects inherit the object’s functionality.
- •In this chapter, you will create the Web site for an online order form
- •In an online store application. The application includes information
- •Ity of building a working online store. Online store classes are very
- •Information and products. The OnlineStore class requires that store
- •Information is stored in a table containing six fields: storeId, name,
- •Information. Instead, the class simply uses session iDs to keep track
- •Variable and function as necessary, without bothering with all this
- •In a class
- •Is developed. Imagine what would happen if Microsoft distributed
- •Ing class is invalid because it does not include an access specifier:
- •If they will not be supported by future xhtml versions or are not
- •Xhtml standards. To review the guide of current w3c css specifi-
- •Information to remind yourself or others of what the code is doing. A
- •Xhtml document to the external style sheet. This link informa-
- •If you select Apache from the WampServer menu and select Service
- •Ing code uses the number_format() function to add comma separa-
- •In data that a user submits to a php script.
- •Value of “On” and the display_startup_errors directive is assigned
- •Instead. By looking at the source code, you could see that the value of
- •Ing engine can even help locate logic errors.
- •In Chapter 8, along with the equivalent mssql_* functions, where
- •Inline styles, 632
- •Xhtml, 620–635 (continued)
The most important thing to remember when sending headers from
PHP is that all of the headers must be sent prior to any Web content.
If even a single character of the Web page is sent prior to sending
the header, the header information will be considered text within
the Web page and not header information. This will prevent your file
from being downloaded. The easiest way to avoid this problem is to
ensure that the first characters on the first line of the PHP script are
the opening PHP tag (<?php).
PHP uses the header() function to return header information to the
client Web browser. The header() function takes a single parameter,
which is a text string containing the name of the header field followed
by a colon, a space, and the data to associate with the header. The
headers listed in Table 5-7 are far from comprehensive, but they are
the only ones you need to download a file.
For example, the following headers tell the Web browser that a file
named info.doc is 5000 bytes long, is encoded using base64 encoding,
and is a file being downloaded:
CHAPTER
5
Working
with Files and Directories
header("Content-Description:
File Transfer");
header("Content-Type:
application/force-download");
header("Content-Disposition:
attachment;
filename=\"info.doc\"");
header("Content-Transfer-Encoding: base64");
header("Content-Length: 5000");
254
The “Content-Type” header can be used in two ways. If you want the
downloaded file to appear in the client’s Web browser as if it were a
normal file, use the MIME type for the file. For example, if you are
downloading a JPEG image, the MIME type would be “image/jpeg”.
The Web browser will display the image. If you want the file to be
saved to the user’s hard drive instead of being opened in the Web
browser, use a MIME type of “application/force-download”, which
instructs the Web browser to open a “save file” dialog box and write
the file to disk.
The third step, once the headers have been sent, is to send the file
itself. The PHP readfile() function reads a file from disk and sends
it directly to the Web browser. The only required parameter for the
readfile() function is a string containing the path and filename to
the file being sent; on success, readfile() returns the number of bytes
sent; on failure, readfile() returns FALSE.
readfile("/usr/uploads/info.doc");
You are finished when the headers have been sent and the readfile()
function has sent the file’s contents. Do not send any XHTML data,
or it will become part of the downloaded file information. If, however,
the headers were not sent and the readfile() function was not called,
the PHP script can create a Web page explaining that the file could
not be downloaded and why. Therefore, your code should ensure that
the headers were not sent and the readfile() function was not called
prior to sending the Web page output.
To create a PHP downloader for the files subdirectory:
1.
2.
Create a new document in your text editor.
Add the following script section to the document body. Be sure
that there is nothing in the file before the opening PHP tag:
<?php
?>
3.
Add the following code to the script section to check if the
requested file exists and is readable:
$Dir = "files";
if (isset($_GET['filename'])) {
$FileToGet = $Dir . "/" . stripslashes
($_GET['filename']);
Uploading
and Downloading Files
if
(is_readable($FileToGet)) {
}
else
{
$ErrorMsg
= "Cannot read \"$FileToGet\"";
$ShowErrorPage = TRUE;
}
}
else {
$ErrorMsg = "No filename specified";
$ShowErrorPage = TRUE;
}
if ($ShowErrorPage) {
255
4.
Add the following code in the if section of the inner
if...else statement for the is_readable() test to download
the file:
header("Content-Description: File Transfer");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment;
filename=\"" . $_GET['filename'] . "\"");
header("Content-Transfer-Encoding: base64");
header("Content-Length: " . filesize($FileToGet));
readfile($FileToGet);
$ShowErrorPage = FALSE;
5.
Add the following code immediately after the closing PHP tag
to show the error page. Note the use of advanced escaping to
display the Web page output.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>File Download Error</title>
<meta http-equiv="content-type" content="text/html;
charset=iso-8859-1" />
</head>
<body>
<p>There was an error downloading "<?php echo
htmlentities($_GET['filename']); ?>"</p>
<p><?php echo htmlentities($ErrorMsg); ?></p>
</body>
</html>
<?php
}
?>
6.
Save the document as FileDownloader.php in the Chapter
directory for Chapter 5 and upload the file to the server.
Reopen ViewFiles.php. Replace the line that reads:
echo "<a href=\"$FullEntryName\">" .
htmlentities($Entry). "</a>\n";
7.

CHAPTER
5
Working
with Files and Directories
with
a line that reads:
echo
"<a
href=\"FileDownloader.php?filename=$Entry\">"
.
htmlentities($Entry). "</a>\n";
8.
256
9.
Save ViewFiles.php and upload the file to the Web server.
Open the ViewFiles.php file in your Web browser by entering
the following URL: http://<yourserver>/PHP_Projects/Chap-
ter.05/Chapter/ViewFiles.php. Click one of the highlighted
filenames. Your Web browser should display a “save file”
dialog box like the one shown in Figure 5-8. Save the file and
verify that it downloaded correctly.
Figure 5-8
The “save file” dialog box for polarbear.gif
10. Close your Web browser window.
Short Quiz
1.
What type of form input element is used to choose the file to
upload?
What hidden form input element restricts the size of the
uploaded file?
What is the name of the autoglobal array that contains the
uploaded file information?
2.
3.
