Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s 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 © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Copyright © 2003 Pearson Education, Inc. Slide 1-1 The Web Wizards Guide to PHP by David A. Lash.
PHP for Server-Side Preprocessing Chapter 08. Overview and Objectives Present a brief history of the PHP language Discuss how PHP fits into the overall.
Your NEW Social Services Verification Tool
More on PHP Coding Lab no. 6 Advance Database Management System.
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
1 Web Wizards Guide To PHP David Lash Chapter 1 Introduction to PHP.
Introduction to PERL Part 4 (1) Working with Files (2)CGI Security.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Reading and Writing Data Files Why do you need reading and writing data files in your CGI programming? Web server does not have permission to create file.
Slide 6a-1 CHAPTER 6 Matching Patterns: Using Regular expressions to match patterns.
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
PHP Tutorials 02 Olarik Surinta Management Information System Faculty of Informatics.
Reading Data in Web Pages tMyn1 Reading Data in Web Pages A very common application of PHP is to have an HTML form gather information from a website's.
Advanced Web 2012 Lecture 4 Sean Costain PHP Sean Costain 2012 What is PHP? PHP is a widely-used general-purpose scripting language that is especially.
Slide 8-1 CHAPTER 8 Using Databases with PHP Scripts: Using MySQL Database with PHP.
Copyright © 2003 Pearson Education, Inc. Slide 1-1 Web Design & Development PHP.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Networking Overview Your OUNet ID ("4 plus 4") OUNet Password Changing Your OUNet Password Your Official OU Forwarding Your Mail Getting Help Overview.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizard’s Guide to PHP by David Lash.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 12.
Copyright © 2003 Pearson Education, Inc. Slide 5-1 The Web Wizard’s Guide to PHP by David Lash.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
INSERT BOOK COVER 1Copyright © 2011 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Microsoft Office Excel 2010 by Robert Grauer, Keith.
Nael Alian Introduction to PHP
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
Introduction to Programming the WWW I CMSC Summer 2004 Lecture 11.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 27 - Phone Book Application Introducing Multimedia.
PHP1-1 PHP Lecture 2 Xingquan (Hill) Zhu
Slide 7-1 CHAPTER 7 Managing Multiple-Form Applications: Writing scripts with multiple screens.
11 1 Cookies CGI/Perl Programming By Diane Zak Objectives In this chapter, you will: Learn the difference between temporary and persistent cookies.
1 Chapter 4 – Breaking It Up: Functions spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Creating PHPs to Insert, Update, and Delete Data CS 320.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
 2008 Pearson Education, Inc. All rights reserved PHP.
Storing and Retrieving Data
5 1 Data Files CGI/Perl Programming By Diane Zak.
The Web Wizard’s Guide To JavaScript Chapter 7 Cookies: Maintaining State.
Copyright © 2003 Pearson Education, Inc. Slide 7-1 The Web Wizard’s Guide to PHP by David Lash.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting Files & Directories.
8 th Semester, Batch 2008 Department Of Computer Science SSUET.
Advanced Web 2012 Lecture 6 Sean Costain Files Sean Costain 2012 Php allows for the : Creation Reading Appending Deleting Uploading And Closing.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
ASP (COMPONENTS) Active Server Pages (cont..) 1. global.asa file The Global.asa file is an optional file that can contain declarations of objects, variables,
Copyright © 2003 Pearson Education, Inc. Slide 6a-1 The Web Wizard’s Guide to PHP by David Lash.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
 A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests.
PHP Syntax You cannot view the PHP source code by selecting "View source" in the browser - you will only see the output from the PHP file, which is plain.
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 The Web Wizard’s Guide to PHP by David A. Lash.
Copyright © 2003 Pearson Education, Inc. Slide 3-1 The Web Wizard’s Guide to PHP by David A. 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 8-1 The Web Wizard’s Guide to PHP by David Lash.
The Web Wizard’s Guide to PHP by David Lash
Section 10.1 Define scripting
Chapter 6 Persistence-Saving and Retrieving Data
Exploring Microsoft Office 2013 Word Comprehensive
<?php require("header.htm"); ?>
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Internet Programming Work with Files in PHP
PHP: Hypertext Preprocessor
The Web Wizard’s Guide to PHP by David A. Lash
Presentation transcript:

Copyright © 2003 Pearson Education, Inc. Slide 6b-1 The Web Wizard’s 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 arrayfile() Each array item will be assigned 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 lines of mydata.txt

Copyright © 2003 Pearson Education, Inc. Slide 6b-6 So if mydata.txt was stored in the same directory as 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 modsfile 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 Once a file is open for reading, use fgets() to read it:fgets()

Copyright © 2003 Pearson Education, Inc. Slide 6b-11 A Full Script Example Consider an example script that invites an end- user to select a 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 Show Source Code Show data file

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

Copyright © 2003 Pearson Education, Inc. Slide 6b-14 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-15 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-16 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-17 PHP Script Show Source Code Show Data File Run Script:

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

Copyright © 2003 Pearson Education, Inc. Slide 6b-19 Using flock PHP provides a flock() function that can ensure only one script at a time writes to a file.flock() Use it in your scripts as a defense against file corruption Use flock($FILEH, LOCK_UN) to unlock a file. Closing a file ( fclose() ) will also release a lock.

Copyright © 2003 Pearson Education, Inc. Slide 6b-20 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-21 Reading and Writing Files When reading and writing from the same file you can use the rewind() function to reset the file pointer to start of file: $ret = rewind(filehandle);

Copyright © 2003 Pearson Education, Inc. Slide 6b-22 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-23 A Full Script Example Consider a script that implements a web page hit counter using the counter file ctr.txt.

Copyright © 2003 Pearson Education, Inc. Slide 6b-24 A Full Script Example: Accessing the counter The script could be placed in a file named counter.php and accessed through an include statement as: For example: Harry's Place Happy Harry's Hardware Home

Copyright © 2003 Pearson Education, Inc. Slide 6b-25 PHP Script Show Source Code Show counter file Show code to be used with include function

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

Copyright © 2003 Pearson Education, Inc. Slide 6b-27 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-28 PHP Script Show Source Code Show Data File

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

Copyright © 2003 Pearson Education, Inc. Slide 6b-30 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.