1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP 5/MySQL Programming- P43 pptx

5 265 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Cấu trúc

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

  • Contents

  • Introduction

  • Chapter 1: Exploring the PHP Environment

  • Chapter 2: Using Variables and Input

  • Chapter 3: Controlling Your Code with Conditions and Functions

  • Chapter 4: Loops and Arrays

  • Chapter 5: Better Arrays and String Handling

  • Chapter 6: Working with Files

  • Chapter 7: Writing Programs with Objects

  • Chapter 8: XML and Content Management Systems

  • Chapter 9: Using MySQL to Create Databases

  • Chapter 10: Connecting to Databases within PHP

  • Chapter 11: Data Normalization

  • Chapter 12: Building a Three-Tiered Data Application

  • Index

  • Team DDU

Nội dung

188 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Examining File Access Modifiers The final parameter in the fopen() command is an access modifier. PHP supports a number of access modifiers, which determine how your program interacts with the file. Files are usually opened for these modes: reading, writing, or appending. Read mode opens a file for input, so your program can read information from the file. You cannot write data to a file that is opened in read mode. Write mode allows you to open a file for output access. If the file does not exist, PHP auto- matically creates it for you. Append mode allows you to write to a file without destroying the current contents. When you write to a file in append mode, all new data is added to the end of the file. You can use a file for random access, which allows a file to be open simultane- ously for input and output, but such files are often not needed in PHP. The rela- tional database techniques provide the same capability with more flexibility and a lot less work. However, the other forms of file access (read, write, and output) are extremely useful, because they provide easy access to the file information. Modifier Type Description “r” Read-only Program can read from the file “w” Write Writes to the file, overwriting it if it already exists “a” Append Writes to the end of the file “r+” “w+” Read and write Random access. Read or write to a specified part of the file TABLE 6.1 FILE ACCESS M ODIFIERS Be very careful about opening a file in write mode. If you open an already existing file for write access, PHP creates a new file and overwrites and destroys the old file’s contents. TRAP IN THE REAL WORLD The “r+” and “w+” modifiers are used for another form of file access, called ran- dom access, which allows simultaneous reading and writing to the same file. While this is a very useful tool, I won’t spend a lot of time on it in this book. The sequential-access methods in this chapter are fine for simple file storage problems; the XML and relational database functions in the remainder of this book aren’t any more difficult than the random access model and provide far more power. Writing to a File The saveSonnet program opens the sonnet76.txt file for write access. If there were already a file in the current directory, it is destroyed. The $fp variable stores the file pointer for the text file. Once this is done, you can use the fputs() function to actually write data to the file. You might be noticing a trend here. Most of the file access functions begin with the letter f: fopen(), fclose(), fputs(), fgets(), feof(). This convention is inherited from the C language. It can help you remember that a particular function works with files. Of course, every statement in PHP that begins with f isn’t necessarily a file function (foreach is a good example), but most function names in PHP that begin with f are file-handling commands. The fputs() function requires two parameters. The first is a file pointer, which tells PHP where to write the data. The second parameter is the text to write out to the file. Closing a File The fclose() function tells the system that your program is done working with the file and should close it. Drive systems are much slower than computer memory and take a long time to spool up to speed. For that reason, when a program encounters an fputs() command, it doesn’t always immediately save the data to a file on the disk. Instead, it adds the data to a special buffer and writes the data only when a suffi- cient amount is on the buffer or the program encounters an fclose() command. This is why it’s important to close your files. If the program ends without encoun- tering an fclose() statement, PHP is supposed to automatically close the file for you, but what’s supposed to happen and what actually happens are often two very different things. Loading a File from the Drive System You can retrieve information from the file system. If you open a file with the “r” access modifier, you can read information from the file. Introducing the loadSonnet.php Program The loadSonnet.php program, shown in Figure 6.6 loads the sonnet saved by saveSonnet.php and displays it as befits the work of the Bard. TRICK TRICK 189 C h a p t e r 6 W o r k i n g w i t h F i l e s The code for the loadSonnet program follows: <html> <head> <title>LoadSonnet</title> <style type = “text/css”> body{ background-color:darkred; color:white; font-family:’Brush Script MT’, script; font-size:20pt } </style> </head> <body> <? $fp = fopen(“sonnet76.txt”, “r”); //first line is title $line = fgets($fp); print “<center><h1>$line</h1></center>\n”; print “<center>\n”; 190 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 6.6 The file has been loaded from the drive system and prettied up a bit with some cascading style sheets (CSS) tricks. //print rest of sonnet while (!feof($fp)){ $line = fgets($fp); print “$line <br>\n”; } // end while print “</center>\n”; fclose($fp); ?> </body> </html> Beautifying Output with CSS CSS styles are the best way to improve text appearance. By setting up a simple style sheet, I very quickly improve the sonnet’s appearance without changing the text. Notice especially how I indicated multiple fonts in case my preferred font was not installed on the user’s system. Using the “r” Access Modifier To read from a file, you must get a file pointer by opening that file for “r” access. If the file does not exist, you get the result FALSE rather than a file pointer. You can open files anywhere on the Internet for read access. If you supply a URL as a filename you can read the URL as if it were a local file. However, you can- not open URL files for output. I opened sonnet76.txt with the fopen() command using the “r” access modifier and again copied the resulting integer to the $fp file pointer variable. Checking for the End of the File with feof() When you are reading data from a file, your program doesn’t generally know the file length. The fgets() command, which gets data from a file, reads one line of the file at a time. Since you can’t be sure how many lines are in a file until you read it, PHP provides a special function called feof(), which stands for file end of file (apparently named by the Department of Redundancy Department). TRICK 191 C h a p t e r 6 W o r k i n g w i t h F i l e s This function returns the value FALSE if any more lines of data are left in the file. It returns TRUE when the program is at the end of the data file. Most of the time when you read file data, you use a while loop that continues as long as feof() is not true. The easiest way to set up this loop is with a statement like this: while (!feof($fp)){ The feof() function requires a file pointer as its sole parameter. Reading Data from the File with fgets() The fgets() function gets one line of data from the file, returns that value as a string, and moves a special pointer to the next line of the file. Usually this func- tion is called inside a loop that continues until feof() is TRUE. Reading a File into an Array It is often useful to work with a file by loading it into an array in memory. Fre- quently you find yourself doing some operation on each array line. PHP provides a couple of features that simplify this type of operation. The cartoonifier.php program demonstrates one way of manipulating an entire file without using a file pointer. Introducing the cartoonifier.php Program The cartoonifier.php program illustrated in Figure 6.7 is a truly serious and weighty use of advanced server technology. This program loads the entire sonnet into an array, steps through each line, and con- verts it to a unique cartoon dialect by performing a search and replace operation. <html> <head> <title>Cartoonify</title> </head> <body> <? $fileName = “sonnet76.txt”; $sonnet = file($fileName); $output = “”; 192 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r . information from the file. Introducing the loadSonnet .php Program The loadSonnet .php program, shown in Figure 6.6 loads the sonnet saved by saveSonnet .php and displays it as befits the work of the Bard. TRICK TRICK 189 C h a p t e r 6 W o r k i n g w i t h F i l e s The. with files. Of course, every statement in PHP that begins with f isn’t necessarily a file function (foreach is a good example), but most function names in PHP that begin with f are file-handling. yourself doing some operation on each array line. PHP provides a couple of features that simplify this type of operation. The cartoonifier .php program demonstrates one way of manipulating an

Ngày đăng: 07/07/2014, 02:20

TỪ KHÓA LIÊN QUAN