Tài liệu PHP: The Good Parts: Delivering the Best of PHP- P6 doc - Pdf 87

This, however, is a dynamically generated form, as you can see in the following code:
<?php
session_start();
$folder = $_SESSION['folder'];
$filename = $folder . "/question1.txt" ;
$file_handle = fopen($filename, "a+");
// open file for reading then clean it out
// pick up any text in the file that may already be there
$comments = fread($file_handle, filesize($filename));
fclose($file_handle); // close this handle
if ($_POST['posted']) {
// create file if first time and then
// save text that is in $_POST['question1']
$question1 = $_POST['question1'];
$file_handle = fopen($filename, "w+");
// open file for total overwrite
if (flock($file_handle, LOCK_EX)) {
// do an exclusive lock
if (fwrite($file_handle, $question1) == FALSE) {
echo "Cannot write to file ($filename)";
}
flock($file_handle, LOCK_UN);
// release the lock
}
// close the file handle and redirect to next page ?
fclose($file_handle);
header( "Location: page2.php" );
} else {
?>
<html>
<head>

this command:
$file_handle = fopen($filename, "a+");
Using the file opening function, fopen(), we ask PHP to provide us with a handle to
that file and store it in the variable suitably called $file_handle. Notice that there is
another parameter passed to the function here: the a+ option. If you look at the PHP
site, you will see a full listing of these option letters and what they mean. This one
causes the file to open for reading and writing, with the file pointer placed at the end.
If the file does not exist, PHP will attempt to create it. If you look at the next two lines
of code, you will see that the entire file is read (using the filesize() function to deter-
mine its size) into the $comments variable, and then it is closed.
$comments = fread($file_handle, filesize($filename));
fclose($file_handle);
Next, we want to see if the form portion of this program file has been executed, and,
if so, we have to save any information that was entered into the text area. This time,
we open the same file again, but we use the w+ option, which causes the interpreter to
open the file for writing only—creating it if it doesn’t exist, or emptying it if it does.
The file pointer is placed at the beginning of the file. Essentially, we want to empty out
the current contents of the file and replace it with a totally new volume of text. For this
purpose, we employ the fwrite() function:
// do an exclusive lock
if (flock($file_handle, LOCK_EX)) {
if (fwrite($file_handle, $question1) == FALSE){
echo "Cannot write to file ($filename)";
}
// release the lock
flock($file_handle, LOCK_UN);
}
We have to be sure that this information is indeed saved into the designated file, so we
wrap a few conditional statements around our file writing operations to make sure
everything will go smoothly. First, we attempt to gain an exclusive lock on the file in

}
flock($file_handle, LOCK_UN); // release the lock
}
// close the file handle and redirect to next page ?
fclose($file_handle);
header( "Location: last_page.php" );
File Management As a Database Alternative | 85
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
} else {
?>
<html>
<head>
<title>Files & folders - On-line Survey</title>
</head>
<body>
<table border=0><tr><td>
Please enter your comments to the following survey statement:
</td></tr>
<tr bgcolor=lightblue><td>
It's a funny thing freedom. I mean how can any of us <br/>
be really free when we still have personal possessions.
How do you respond to the previous statement?
</td></tr>
<tr><td>
<form action="<?= $PHP_SELF ?>" method=POST>
<input type="hidden" name="posted" value=1>
<br/>
<textarea name="question2" rows=12 cols=35><?= $comments ?></textarea>
</td></tr>
<tr><td>

File Management As a Database Alternative | 87
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.
CHAPTER 8
PHP and Friends
PHP is a wonderful language—it is robust and flexible and friendly. By friendly, I mean
that it can freely integrate with libraries built by outside sources. This is in keeping with
an important and ever-present caveat in the open source development world: not re-
inventing the wheel. There are many different libraries out on the Web that can inte-
grate well with PHP and are actually also developed in PHP. In this chapter, we will
look at three different PHP add-on libraries and discuss how to use existing tools to
enhance our PHP web development.
The three libraries we’ll cover are all PHP object-oriented-based, so be sure you have
read Chapter 6 in this book or are familiar with object-oriented programming before
going too far into the examples. These three libraries were chosen because they are
helpful for performing some of the top tasks in a modern web-based application: send-
ing email messages or Short Message Service (SMS) text messages, generating PDF
forms, and generating graphical data reports (e.g., pie charts and bar charts).
Email/SMS Generation
PHP has a built-in mail function called
mail()
. This will send out Simple Mail Transport
Protocol (SMTP) mail to the world. The mail function is quite simplistic and basic, so
it usually is not the best choice, on its own, for heavier email tasks. It’s tricky to send
email messages with attachments, for example.
The PHP library, called PHPMailer, is just what the doctor ordered to fill the gap. It is
object-based and you can add it easily into a script with either an
include
or, more
appropriately, a

// name is optional
$mail->AddAddress("", "Josh Adams");
$mail->AddAddress("");
$mail->AddReplyTo("", "Information");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// add attachments
$mail->AddAttachment("/var/tmp/file.tar.gz");
// optional attachment file name
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message
body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for
non-HTML mail clients";
90 | Chapter 8: PHP and Friends
Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark.


Nhờ tải bản gốc
Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status