Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash.

Similar presentations


Presentation on theme: "Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash."— Presentation transcript:

1

2 Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash

3 Copyright © 2003 Pearson Education, Inc. Slide 6b-2 CHAPTER 6 Working with Files: Using files on the Web Server From PHP Scripts

4 Copyright © 2003 Pearson Education, Inc. Objectives To learn to work with files to store and retrieve data To understand the types of applications that can use File I/O

5 Copyright © 2003 Pearson Education, Inc. Slide 6b-4 Why Use Files Files can provide an easy mechanism for storing data between executions of scripts: store customer data store page hit counts remember end-user preferences store product inventory

6 Copyright © 2003 Pearson Education, Inc. Slide 6b-5 Reading A file the easy way. You can use the file() function to read an entire file into a PHP array Each array item will be assign 1 line For example: $inf ='mydata.txt'; $infile = file ($inf); print "$infile[0]"; print "$infile[2]"; Open mydata.txt and read into $infile array. Output the1st and then 3rd line of mydata.txt

7 Copyright © 2003 Pearson Education, Inc. Slide 6b-6 So if mydata.txt was stored in same directory at your PHP script and contained: Apples are red. Bananas are yellow. Carrots are orange. Dates are brown. Would output: Apples are red. Carrots are orange. Using file() works well but does consume memory Can slow scripts down for large files Reading A file the easy way.

8 Copyright © 2003 Pearson Education, Inc. Slide 6b-7 Using fopen() to open files Use the fopen() script to create a connection to a file once connected can read or write a line at a time

9 Copyright © 2003 Pearson Education, Inc. Slide 6b-8 Using file open mods

10 Copyright © 2003 Pearson Education, Inc. Slide 6b-9 Dealing with fopen() failure The fopen() function can fail for a number of reasons: file location, file permissions, corrupt file Provide an option for fopen() to output a message if it fails: $inf = '/home/phppgm/data/mydata.txt'; $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); Only run die() when fopen() fails.

11 Copyright © 2003 Pearson Education, Inc. Slide 6b-10 Using fgets() to read Open you open a file for reading use fgets() to read it:

12 Copyright © 2003 Pearson Education, Inc. Slide 6b-11 A Full Script Example Consider an example script that invites end-user to select product number: Provides information about the product. Uses the following HTML form input line: Also uses the following input file: AC1000:Hammers:122:12.50 AC1001:Wrenches:5:5.00 AC1002:Handsaws:10:10.00 AC1003:Screwdrivers:222:3.00

13 Copyright © 2003 Pearson Education, Inc. Slide 6b-12 PHP Script 1. Hardware Inventory 2. Happy Harry's Hardware Inventory 3.

14 Copyright © 2003 Pearson Education, Inc. Slide 6b-13 PHP Script With REGISTER_GLOBALS Off 1. Hardware Inventory 2. Happy Harry's Hardware Inventory 3.

15 Copyright © 2003 Pearson Education, Inc. Slide 6b-14 The Output... The previous code can be executed at http://webwizard.aw.com/~phppgm/C6/drive_invent.php http://webwizard.aw.com/~phppgm/C6/drive_invent.php

16 Copyright © 2003 Pearson Education, Inc. Slide 6b-15 Writing to Files You can use the fputs() function The above outputs: My script was here into the file that $OFILE points to

17 Copyright © 2003 Pearson Education, Inc. Slide 6b-16 Putting it all together Consider the following script that: opens a file, writes one line to it closes the file $inf ='/home/phppgm/data/log.txt'; $FILEH = fopen($inf, 'w') or die("Cannot open $inf"); fputs($FILEH, 'Apples are red'); fclose ($FILEH);

18 Copyright © 2003 Pearson Education, Inc. Slide 6b-17 A Full Script Example Consider another example that appends end- user comments to the end of a file: Asks end-user for comments about site. Uses the following HTML form input line:

19 Copyright © 2003 Pearson Education, Inc. Slide 6b-18 PHP Script 1. Log Comments 2. 11.

20 Copyright © 2003 Pearson Education, Inc. Slide 6b-19 Locking a File Web application have potential for many users to execute same script at same time If scripts attempt to write the same file at same instance in time, may corrupt the file A corrupted file is a useless unintelligible mixture of data.

21 Copyright © 2003 Pearson Education, Inc. Slide 6b-20 Using flock PHP provide a flock() function that can ensure only 1 script at a time writes to a file. Use it in your scripts as a defense against file corruption

22 Copyright © 2003 Pearson Education, Inc. Slide 6b-21 Tip: Using the newline character \n Use the \n character to output a new line character into the file. If you omit the new line character, the appended text will appear together on the same line if you run the script twice, for example: My script was hereMy script was here If your script puts an \n at the lines end: My script was here

23 Copyright © 2003 Pearson Education, Inc. Slide 6b-22 Reading and Writing Files When reading and writing from same file can use the rewind() function: Resets the file pointer to start of file: $ret = rewind(filehandle); For example consider: 1. $FILEH = fopen('myfile.txt', 'r+'); 2. $inline = fgets($FILEH, 4096); 3. rewind($FILEH); 4.$ret= fputs($FILEH, 'Z');

24 Copyright © 2003 Pearson Education, Inc. Slide 6b-23 Reading and Writing Files For example consider: 1. $FILEH = fopen('myfile.txt', 'r+'); 2. $inline = fgets($FILEH, 4096); 3. rewind($FILEH); 4.$ret= fputs($FILEH, 'Z'); Suppose myfile.txt contains: A B C After script segment runs, the file will contain: Z B C

25 Copyright © 2003 Pearson Education, Inc. Slide 6b-24 A Full Script Example Consider a script that implements a web page hit counter: Use counter file at /home/phppgm/logfiles/ctr.txt. Can place into this file and access it as:

26 Copyright © 2003 Pearson Education, Inc. Slide 6b-25 A Full Script Example: Accessing the counter Can place into this file and access it as: For example: Harry's Place Happy Harry's Hardware Home Hit Count:

27 Copyright © 2003 Pearson Education, Inc. Slide 6b-26 PHP Script 1.

28 Copyright © 2003 Pearson Education, Inc. Slide 6b-27 The Output... The previous code can be executed at http://webwizard.aw.com/~phppgm/C6/drivecounter.html http://webwizard.aw.com/~phppgm/C6/drivecounter.html

29 Copyright © 2003 Pearson Education, Inc. Slide 6b-28 Another Full Script Example Consider another example that implements an on-line survey: Asks end-user to select favorite tool Hammer Wrench Screwdriver Frying Pan Use survey file /home/phppgm/data/survey1.txt. 0:0:0:0 initial value

30 Copyright © 2003 Pearson Education, Inc. Slide 6b-29 A Full Script Example: Accessing the counter Can place into this file and access it as: For example: Harry's Place Happy Harry's Hardware Home Hit Count:

31 Copyright © 2003 Pearson Education, Inc. Slide 6b-30 PHP Script 1. Happy Harry's Hardware Survey 2. 3. Survey Results 4.

32 Copyright © 2003 Pearson Education, Inc. Slide 6b-31 PHP Script with REGISTER_GLOBALS Off 1. Happy Harry's Hardware Survey 2. 3. Survey Results 4.

33 Copyright © 2003 Pearson Education, Inc. Slide 6b-32 The Output... The previous code can be executed at http://webwizard.aw.com/~phppgm/C6/drivesurvey.html http://webwizard.aw.com/~phppgm/C6/drivesurvey.html

34 Copyright © 2003 Pearson Education, Inc. Slide 6b-33 Summary Working with files enable scripts to store data for long periods of time. file() - read file into an array fopen() - open a file fgets() - read a line from file fputs() - write to a file. flock() - lock access to file.


Download ppt "Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash."

Similar presentations


Ads by Google