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

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizards Guide to PHP by David Lash.
Advertisements

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to XHTML Programming the World Wide Web Fourth edition.
Advanced Piloting Cruise Plot.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizards Guide to PHP by David Lash.
1 Copyright © 2002 Pearson Education, Inc.. 2 Chapter 2 Getting Started.
Copyright © 2003 Pearson Education, Inc. Slide 1-1 The Web Wizards Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 Created by Cheryl M. Hughes The Web Wizards Guide to XML by Cheryl M. Hughes.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizards Guide to PHP by David Lash.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
1 Copyright © 2013 Elsevier Inc. All rights reserved. Appendix 01.
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
UNITED NATIONS Shipment Details Report – January 2006.
Chapter 3: Linked List Tutor: Angie Hui
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Custom Services and Training Provider Details Chapter 4.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Year 6 mental test 10 second questions
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Using Family Connection Online Resource for Planning & Advising.
Chapter 17 Linked Lists.
ABC Technology Project
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 12 – Security Panel Application Introducing.
XP New Perspectives on Introducing Microsoft Office 2003 Tutorial 1 1 Using Common Features of Microsoft Office 2003 Tutorial 1.
Green Eggs and Ham.
VOORBLAD.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
BIOLOGY AUGUST 2013 OPENING ASSIGNMENTS. AUGUST 7, 2013  Question goes here!
Factor P 16 8(8-5ab) 4(d² + 4) 3rs(2r – s) 15cd(1 + 2cd) 8(4a² + 3b²)
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Understanding Generalist Practice, 5e, Kirst-Ashman/Hull
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
25 seconds left…...
Januar MDMDFSSMDMDFSSS
Analyzing Genes and Genomes
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 12 Working with Forms Principles of Web Design, 4 th Edition.
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Immunobiology: The Immune System in Health & Disease Sixth Edition
Essential Cell Biology
Energy Generation in Mitochondria and Chlorplasts
CpSc 3220 Designing a Database
What’s new in WebSpace Changes and improvements with Xythos 7.2 Effective June 24,
Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
Internet Programming Work with Files in PHP
Presentation transcript:

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

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

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

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

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

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.

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

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

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.

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

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

Copyright © 2003 Pearson Education, Inc. Slide 6b-12 PHP Script 1. Hardware Inventory 2. Happy Harry's Hardware Inventory 3. <?php 4. $inf='/home/phppgm/data/infile.txt'; 5. $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); 6. $inline = fgets($FILEH, 4096); 7. $found=0; 8. while (!feof($FILEH) && !($found)) { 9. list($ptno,$ptname,$num,$price) = split (':',$inline); 10. if ($ptno == $id) { 11. print ' '; 12. print ' ID Part Count Price '; 13. print " $ptno $ptname "; 14. print " $num \$$price "; 15. print ' '; 16. $found = 1; 17. } 18. $inline = fgets($FILEH, 4096); 19. } 20. if ($found != 1) { 21. print "Error: PartNo=$id not found"; 22. } 23. fclose ($FILEH); 24.?>

Copyright © 2003 Pearson Education, Inc. Slide 6b-13 PHP Script With REGISTER_GLOBALS Off 1. Hardware Inventory 2. Happy Harry's Hardware Inventory 3. <?php $id = $_POST = [id]; 4. $inf='/home/phppgm/data/infile.txt'; 5. $FILEH = fopen($inf, 'r') or die ("Cannot open $inf"); 6. $inline = fgets($FILEH, 4096); 7. $found=0; 8. while (!feof($FILEH) && !($found)) { 9. list($ptno,$ptname,$num,$price) = split (':',$inline); 10. if ($ptno == $id) { 11. print ' '; 12. print ' ID Part Count Price '; 13. print " $ptno $ptname "; 14. print " $num \$$price "; 15. print ' '; 16. $found = 1; 17. } 18. $inline = fgets($FILEH, 4096); 19. } 20. if ($found != 1) { 21. print "Error: PartNo=$id not found"; 22. } 23. fclose ($FILEH); 24.?>

Copyright © 2003 Pearson Education, Inc. Slide 6b-14 The Output... The previous code can be executed at

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

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);

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:

Copyright © 2003 Pearson Education, Inc. Slide 6b-18 PHP Script 1. Log Comments 2.<?php 3. $logfile='/home/phppgm/data/comments.txt'; 4. $OUTF = fopen($logfile, 'a+') or die("Cannot open $logfile"); 5. $today = date('m/d/Y:h:m'); 6. $msg = "$today:$comments\n"; 7. print "Just Logged: $msg"; 8. fputs($OUTF, $msg); 9. fclose($OUTF); 10.?> 11.

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.

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

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

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');

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

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:

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:

Copyright © 2003 Pearson Education, Inc. Slide 6b-26 PHP Script 1.<?php 2.$ctfile='/home/phppgm/logfiles/ctr.txt'; 3.$FILEH = fopen($ctfile, 'r+') or die ("Cannot open $ctfile"); 4.flock($FILEH, LOCK_EX) or die ("Cannot lock file $outf"); 5.$ctr = fgets($FILEH, 4096) or die ("Cannot gets $ctfile"); 6.$ctr = rtrim($ctr, '\n'); 7.if (is_numeric($ctr)){ 8. $count= $ctr + 1; 9. rewind ($FILEH); 10. $ret= fputs($FILEH, $count); 11. print ("$count"); 12.} else { 13. print "Error: ctr=$ctr <=not numeric value"; 14.} 15.fclose ($FILEH); 16.?>

Copyright © 2003 Pearson Education, Inc. Slide 6b-27 The Output... The previous code can be executed at

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

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:

Copyright © 2003 Pearson Education, Inc. Slide 6b-30 PHP Script 1. Happy Harry's Hardware Survey Survey Results 4.<?php 5.$ctfile='/home/phppgm/data/survey1.txt'; 6.$SURVEY = fopen($ctfile, 'r+') or die("Cannot open $ctfile"); 7.flock($SURVEY, LOCK_EX) or die ("Cannot lock file $outf"); 8.$inline = fread($SURVEY, 4096); 9.list($hammer, $wrench, $sdriver, $fpan) = split(':', $inline); 10.if ($tool == 'hammer'){ 11. $hammer = $hammer + 1; 12.} elseif ($tool == 'sdriver') { 13. $sdriver = $sdriver + 1; 14.} elseif ($tool == 'wrench'){ 15. $wrench = $wrench + 1; 16.} elseif ($tool == 'fryingpan'){ 17. $fpan = $fpan + 1; 18.} else { 19. die ('ERROR: Illegal call'); 20.} 21.$ct=$hammer + $sdriver + $wrench + $fpan; 22.print " Hammer=$hammer Wrench=$wrench Screwdriver=$sdriver Frying Pan=$fpan Total votes=$ct"; 23.rewind($SURVEY); 24.$ret= fputs($SURVEY, "$hammer:$wrench:$sdriver:$fpan"); 25. fclose ($SURVEY);?>

Copyright © 2003 Pearson Education, Inc. Slide 6b-31 PHP Script with REGISTER_GLOBALS Off 1. Happy Harry's Hardware Survey Survey Results 4.<?php $tool = $_POST[tool]; 5.$ctfile='/home/phppgm/data/survey1.txt'; 6.$SURVEY = fopen($ctfile, 'r+') or die("Cannot open $ctfile"); 7.flock($SURVEY, LOCK_EX) or die ("Cannot lock file $outf"); 8.$inline = fread($SURVEY, 4096); 9.list($hammer, $wrench, $sdriver, $fpan) = split(':', $inline); 10.if ($tool == 'hammer'){ 11. $hammer = $hammer + 1; 12.} elseif ($tool == 'sdriver') { 13. $sdriver = $sdriver + 1; 14.} elseif ($tool == 'wrench'){ 15. $wrench = $wrench + 1; 16.} elseif ($tool == 'fryingpan'){ 17. $fpan = $fpan + 1; 18.} else { 19. die ('ERROR: Illegal call'); 20.} 21.$ct=$hammer + $sdriver + $wrench + $fpan; 22.print " Hammer=$hammer Wrench=$wrench Screwdriver=$sdriver Frying Pan=$fpan Total votes=$ct"; 23.rewind($SURVEY); 24.$ret= fputs($SURVEY, "$hammer:$wrench:$sdriver:$fpan"); 25. fclose ($SURVEY);?>

Copyright © 2003 Pearson Education, Inc. Slide 6b-32 The Output... The previous code can be executed at

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.