Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Xhtml hyperlink. To download a file from outside the xhtml

directory structure, use the header() function to identify the file

and the readfile() function to send the file contents to the Web

browser.

• The

file_put_contents() function writes or appends a text string

to a file. The file_get_contents() function reads a file into a text

string. The file() function reads a file into an indexed array.

• The stream is used for accessing a resource from which you can

read and to which you can write. The input stream reads data from

a resource such as a file, whereas the output stream writes data to a

resource (again, such as a file).

• The

fopen() function opens a stream to a text file.

• A file pointer is a special type of variable that refers to the cur-

rently selected line or character in a file.

• When you finish working with a file stream, you use the

fclose()

function to ensure that the file doesn’t keep taking up space in your

computer’s memory.

• The

fwrite() function incrementally writes data to a text file.

• The

flock() function prevents multiple users from modifying a file

simultaneously.

284


Comprehension Check

• The

feof() function determines if the file pointer is at the end of

the file.

• PHP includes various functions, such as the

fread() and

fgets() functions, that allow you to use the file pointer to

iteratively read a text file.

• The

copy() function copies a file. The rename() function renames

or moves a file or directory.

• The

unlink() function deletes files, and the rmdir() function

deletes directories.

• PHP does not contain a separate command for moving files.

Instead, you can rename the file with the rename() function and

specify a new directory where you want to store the renamed file.

Or, you must copy the file with the copy() function, and then

delete the original file with the unlink() function.

285

Comprehension Check

1.

Which of the following escape sequences is used on

Macintosh platforms? (Select all that apply.)

a. \n

b. \r

c. \n\r

d. \r\n

2.

Which of the following functions sorts directory entries?

a. scandir()

b. readdir()

c. opendir()

d. sortdir()

3.

Explain when you should use file and directory status func-

tions such as file_exists() and is_dir().

What is the value of the enctype attribute for a Web form

that uploads a file?

What is the name of the autoglobal array that contains

uploaded file information?

4.

5.


CHAPTER 5

Working with Files and Directories

6.

Which of the following constants can you use with the

file_put_contents() function to append data to the end of a

file?

a. INCLUDE_FILE

286

b. FILE_USE_INCLUDE_PATH

c. APPEND

d. FILE_APPEND

7.

Which of the following functions reads the contents of a file

into a string?

a. file()

b. file_get_contents()

c. fread()

d. readfile()

8.

The file() function automatically recognizes whether the

lines in a text file end in \n, \r, or \n\r. True or False?

Which of the following allows you to read data from a

resource such as a file?

a. an input stream

b. an output stream

c. a pointer

d. a reference

10. Which of the following best describes the "w+" method

argument?

a. creates and opens the specified file for reading and writ-

ing; returns

FALSE if the file already exists

b. opens the specified file for reading and writing and places

the file pointer at the end of the file; attempts to create the

file if it doesn’t exist

c. opens the specified file for writing only and deletes any

existing content in the file; attempts to create the file if it

doesn’t exist

d. opens the specified file for reading and writing and deletes

any existing content in the file; attempts to create the file

if it doesn’t exist

9.


Comprehension Check

11. Ais a special type of variable that refers to

the currently selected character in a file.

a. character pointer

b. line pointer

c. file pointer

d. directory pointer

12. Explain why you should call the fclose() function when you

are finished working with a file.

13. You must open and close a file stream when you use the

file_put_contents() function. True or False?

14. What is the correct syntax for using the fwrite() function to

write a value of “Forestville Foods\n” to a file handle named

$SalesProspects?

a. $SalesProspects = fwrite("Forestville Foods\n");

b. fwrite($SalesProspects, "Forestville Foods\n");

c. fwrite("Forestville Foods\n", $SalesProspects);

d. fwrite("$SalesProspects, Forestville Foods\n");

15. Explain why you should lock files before writing data to

them.

16. Which of the following operational constants can you use

with the

flock() function? (Choose all that apply.)

a. LOCK_EX

b. LOCK_NH

c. LOCK_SH

d. LOCK_UH

17. Which of the following functions can you use to iterate

through a text file? (Choose all that apply.)

a. stream_get_line()

b. fgets()

c. fread()

d. readfile()

287


CHAPTER 5

Working with Files and Directories

18. Which of the following functions returns a value of

TRUE when a file pointer reaches the end of a file?

a. is_end()

b. end()

288

c. eof()

d. feof()

19. Which of the following statements creates a directory named

“students” at the same level as the current directory?

a. mkdir("/students");

b. mkdir("students");

c. mkdir("/students/");

d. mkdir("../students");

20. Explain the two ways in which you can move a file with PHP.

Reinforcement Exercises

Exercise 5-1

In this project, you will create a hit counter script that keeps track

of the number of hits a Web page receives. Ensure that the Projects

directory has read and write permissions for everyone.

1.

Create a new document in your text editor and type the

<!DOCTYPE> declaration, <html> element, document head, and

<body> element. Use the strict DTD and “Hit Counter” as the

content of the <title> element.

Add the following script section to the document body:

<?php

?>

2.

3.

Add the following statement to the script section to declare a

variable named $CounterFile that contains the name of the

file where the hits will be stored:

$CounterFile = "hitcount.txt";

4.

Add the following if statement to the end of the script sec-

tion. The if statement determines whether the hitcount.txt


Reinforcement Exercises

file already exists. If it does, the file_get_contents() func-

tion retrieves the value from the file and increments it by 1.

if (file_exists($CounterFile)) {

$Hits = file_get_contents($CounterFile);

++$Hits;

}

289

5.

Add the following else statement to the end of the script

section. The else statement contains a single statement that

assigns a value of 1 to the $Hits variable in the event that the

hitcount.txt file has not yet been created.

else

$Hits = 1;

6.

Finally, add the following statements to the end of the script

section. The echo statement displays the number of hits and

the if statement updates the value in the hitcount.txt file.

Remember that the file_put_contents() function opens the

file if it already exists or creates the file if it doesn’t exist.

echo "<h1>There have been $Hits hits to this page.

</h1>\n";

if (file_put_contents($CounterFile, $Hits))

echo "<p>The counter file has been updated.

</p>\n";

7.

Save the document as HitCounter.php in the Projects direc-

tory for Chapter 5.

Open HitCounter.php in your Web browser by entering

the following URL: http://<yourserver>/PHP_Projects/

Chapter.05/Projects/HitCounter.php. The first time you open

the Web page, you should see a hit count of 1. Reload the Web

page a few times to see if the count increases.

Close your Web browser window.

8.

9.

Exercise 5-2

In this project, you will create a Web page that allows visitors to your

site to sign a guest book that is saved to a text file. Ensure that the

Projects directory has read and write permissions for everyone.

1.

Create a new document in your text editor and type the

<!DOCTYPE> declaration, <html> element, document head, and

<body> element. Use the strict DTD and “Guest Book” as the

content of the <title> element.


CHAPTER 5

Working with Files and Directories

2.

Add the following text and elements to the document body:

<h2>Enter your name to sign our guest book</h2>

<form method="POST" action="SignGuestBook.php">

<p>First Name <input type="text" name="first_name"

/></p>

<p>Last Name <input type="text" name="last_name"

/></p>

<p><input type="submit" value="Submit" /></p>

</form>

<p><a href="ShowGuestBook.php">Show Guest Book

</a></p>

290

3.

Save the document as GuestBook.html in the Projects direc-

tory for Chapter 5.

Create a new document in your text editor and type the

<!DOCTYPE> declaration, <html> element, document head, and

<body> element. Use the strict DTD and “Sign Guest Book” as

the content of the <title> element.

Add the following script section to the document body:

<?php

?>

4.

5.

6.

Add the following if statement to the script section to check

whether the user filled in the first name and last name fields:

if (empty($_POST['first_name']) || empty($_

POST['last_name']))

echo "<p>You must enter your first and last

name. Click your browser's Back button to

return to the Guest Book.</p>\n";

7.

Add the following else clause to the end of the script section.

The statements in the else clause use the fwrite() function

to add visitor names to a text file named guestbook.txt.

else {

$FirstName = addslashes($_POST['first_name']);

$LastName = addslashes($_POST['last_name']);

$GuestBook = fopen("guestbook.txt", "ab");

if (is_writeable("guestbook.txt")) {

if (fwrite($GuestBook, $LastName . ", " .

$FirstName . "\n"))

echo "<p>Thank you for signing our

guest book!</p>\n";

else

echo "<p>Cannot add your name to the

guest book.</p>\n";

}


Reinforcement Exercises

else

echo "<p>Cannot write to the file.</p>\n";

fclose($GuestBook);

}

8.

Save the document as SignGuestBook.php in the Projects

directory for Chapter 5.

Create a document named ShowGuestBook.php that dis-

plays the names of visitors who have signed the guest book.

Use the readfile() function to display the contents of the

guestbook.txt file. Note that you will need to use the <pre>

element for Web browsers to recognize the line breaks.

291

9.

10. Open GuestBook.html in your Web browser by enter-

ing the following URL: http://<yourserver>/PHP_Projects/

Chapter.05/Projects/GuestBook.html. Test the form to see if

you can write data to and read data from the guestbook.txt

file.

11. Close your Web browser window.

Exercise 5-3

Create a document with a form that registers bowlers for a bowl-

ing tournament. Use a single text file that saves information for each

bowler on a separate line. Include the bowler’s name, age, and aver-

age, separated by commas. Ensure that the Projects directory has read

and write permissions for everyone.

Exercise 5-4

Create a Web page to be used for storing software development bug

reports in text files. Include fields such as product name and ver-

sion, type of hardware, operating system, frequency of occurrence,

and proposed solutions. Include links on the main page that allow

you to create a new bug report and update an existing bug report.

Ensure that the Projects directory has read and write permissions for

everyone.

Exercise 5-5

Create a Web form for uploading pictures for a high school reunion.

The form should have text input fields for the person’s name and


Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]