1 / 29

DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida

Media Software Design. DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida. The Objectives: Learn how to read a text file into a PHP script. Learn how create a text file from data in a PHP script. Why do I care about Files?.

scott
Download Presentation

DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Media Software Design DIG 3134 Lecture 8: Reading and Writing Text Files Michael Moshell University of Central Florida

  2. The Objectives: • Learn how to read a text file • into a PHP script. • Learn how create a text file from • data in a PHP script.

  3. Why do I care about Files? Jaindiamondtools.com • Lots of data is available in text files • Price tables • Customer lists • Addresses, e-mail lists • Setting up a complex program is often easier • if you use a file. • Example: Wheel of Fortune: • -- read the cliche’s from a text file • -- easily modified to improve gameplay 3 3 3 3 3 3 3

  4. File Paths: Relative Fulltimewow.blogspot.com $filename1="cats.txt"; // file is where script is. $filename2="images/cat1.jpg" // file is in sub-directory $filename3="../species/persian.txt" // in SIBLING directory scripts myprogram.php  this script contains that stuff cats.txt images cat1.jpg petstore.php species persian.txt directories are shown in italics 4 4 4 4 4 4 4 4

  5. File Paths: absolute, on PC: Fulltimewow.blogspot.com $filename1="c:/wamp/www/cats.txt"; // file is where script is. $filename2="c:/wamp/www/images/cat1.jpg" // file is in subdir. $filename3="c:/wamp/www/species/persian.txt" // in SIBLING dir. c: wamp www  docroot - - where Apache looks for html & php scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt 5 5 5 5 5 5 5 5 5

  6. File Paths absolute: Mac Fulltimewow.blogspot.com $filename1="/Applications/MAMP/htdocs/cats.txt"; $filename2="/Applications/MAMP/htdocs//images/cat1.jpg"; $filename3="/Applications/MAMP/htdocs//species/persian.txt"; Applications MAMP htdocs docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt 6 6 6 6 6 6 6 6 6 6

  7. File Paths absolute: Unix/Linux (e. g. Sulley) $filename1="/home/faculty/moshell/public_html/scripts/cats.txt"; $filename2="/home/faculty/moshell/public_html/scripts/images/cat1.jpg"; $filename3="/home/faculty/moshell/public_html/species/persian.txt"; /home faculty moshell public_html  docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt 7 7 7 7 7 7 7 7 7 7 7

  8. File Paths absolute: Unix/Linux (e. g. Sulley) $filename1="/home/student/aa34567/public_html/scripts/cats.txt"; $filename2="/home/student/aa34567/public_html/scripts/images/cat1.jpg"; $filename3="/home/student/aa34567/public_html/species/persian.txt"; /home student aa34567 public_html  docroot scripts myprogram.php cats.txt images cat1.jpg petstore.php species persian.txt 8 8 8 8 8 8 8 8 8 8 8

  9. Why would we need absolute paths? Fulltimewow.blogspot.com • Example: • Perhaps you host websites for three different companies, but • all three need access to common tax tables. • The relative paths from the boutiques, and from Pandaboots, • is different. • So, all could use this absolute path: • Docroot • PandaBoots $file="c:/wamp/www/taxtables/federal.txt" • boutiques • library.forboutiques • Sammys • Tanqueria • taxtables • federal.txt 9 9 9 9 9 9 9 9 9 9 9

  10. Code for use on both PC and Mac Fulltimewow.blogspot.com • Use relative paths. Then everything starts from 'docroot' • BUT if your system needs absolute paths, do it this way: • //$Docroot="c:/wamp/www/"; // PC version • $Docroot="/Applications/MAMP/htdocs/"; // Mac version • $file=$Docroot."taxtables/federal.txt"; 10 10 10 10 10 10 10 10 10 10 10 10

  11. Code for use on both PC and mac Fulltimewow.blogspot.com • Use relative paths. Then everything starts from 'docroot' • BUT if your system needs absolute paths, uncomment the right one: • //$Docroot="c:/wamp/www/"; // PC version • $Docroot="/Applications/MAMP/htdocs/"; // Mac version • $file=$Docroot."taxtables/federal.txt"; • Why not "$Docroottaxtables/federal.txt"; ? • Because PHP will look for a variable $Docroottaxtables • and come up with nothing. 11 11 11 11 11 11 11 11 11 11 11 11 11

  12. Aside: why $Docroot not $docroot? Fulltimewow.blogspot.com I use a convention about global variables: Start their names with $Capital letters. It helps to make global variable instantly recognizable. Anything that helps my poor old memory is a good thing!! 12 12 12 12 12 12 12 12 12 12 12 12 12

  13. A better way to switch systems Fulltimewow.blogspot.com $Computing_environment='pc'; // or 'mac' or 'sulley'; // .. And somewhere far below, in the code: if ($Computing_environment=='pc') $Docroot="c:/wamp/www/"; else if ($Computing_environment=='mac') $Docroot="/Applications/MAMP/htdocs/"; else if ($Computing_environment=='sulley') $Docroot="/home/faculty/moshell/public_html/"; else print "Error 221: unknown computing environment was ". "specified:$Computing_environment"; 13 13 13 13 13 13 13 13 13 13 13 13 13

  14. What's a text file? Lib.utexas.edu It consists of a series of lines of text with some kind of a line separator character (one or more) at the end of each line. The character used is different on PC and on MAC. But PHP will normally 'smooth out' the difference and hide it. The "official character" for line separation is a newline, which can be represented by "\n". Text files can contain numbers, letters and special characters. An important invisible character is a TAB or "\t". 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14

  15. A Mac-specific problem Mac treats end-of-line differently. To make sure that the 'file' function reads ALL the lines of a text file, put this at the head of your code: <?php session_start(); // this should be there anyway ini_set ("auto_detect_line_endings", true); 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 15

  16. Our design goal: Lib.utexas.edu • Make a simple program that • Displays a text • Accepts one new line of input • Writes out the text including the new line. • The program will have only two screens: • Screen 1: display the text, and two buttons. UPDATE and QUIT. • Screen 2: If QUIT was selected, report that the text was saved. 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16

  17. Step 1: Build a text-reading function EXAM ALERT: Look up and UNDERSTAND Each of the Green built-in functions #textloader: function textloader($whatfile) { if (!file_exists($whatfile)) { print "Sorry, can't find $whatfile."; exit; } else { $textarray=file($whatfile); return $textarray; } }# textloader 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17

  18. Step 2: Build a text-display function #textdisplay: function textdisplay($intextarray) { // each item in the $textarray is one line of text. $linecount=count($intextarray) for ($i=0; $i<$linecount; $i++) print $intextarray[$i]."<br />"; }# textdisplay 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18

  19. Step 3: Get that much working (filedemo1.php) Then build a file writing function. (NOTE: Filedemo1 and filedemo2 in ONE text file named filedemo.txt, with filedemo3 too!) function textsaver($whatfile,$whattext) { $fp = fopen($whatfile, 'w'); // the 'w' means 'write over the previous contents.' $arraysize=count($whattext); for ($i=0; $i<$arraysize; $i++) fwrite($fp, $whattext[$i]); fclose($fp); } # textsaver 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

  20. Putting it all together: Examine filedemo2.php Some useful functions like makeheader, makefooter The file i/o functions The classical if-then-else chain *** you may steal any or all of this code for use in your Wheel of Fortune program **** >>>> You must understand everything in this program, for mastery of the Midterm Exam <<<<< 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

  21. Another example:studentlist.php I have a class roster (list of student names and NIDS) I want a class link-list (HTML document to link me to each person's Sulley account) via URLs like http://sulley.cah.ucf.edu/~ni123456 So I created studentlist.php Examine this code ... understand how it works  MTX Material! 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21 21

  22. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

  23. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: here’s one way: for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i]=substr($theline,$i,1); Advantage: I built this because I didn’t know any automatic way to do it. 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23 23

  24. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: here’s another way: $thecharacters=str_split($theline); Disadvantage: you have to know about str_split, a built-in function. 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24 24

  25. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: A useful test function: print_r ("Print recursive") $thecharacters=str_split($theline); Testing the concept: the print_r function. print_r($thecharacters); Array ( [0] => W [1] => o [2] => m [3] => b [4] => a [5] => t [6] => s [7] => [8] => h [9] => a [10] => v [11] => e [12] => [13] => n [14] => o [15] => [16] => w [17] => i [18] => n [19] => g [20] => s ) 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25

  26. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: • Disadvantage of str_split: What if you wanted • to push the character in string position 0 • into the array at location 1? • Like THIS? 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26 26

  27. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: • Disadvantage of str_split: What if you wanted • to push the character in string position 0 • into the array at location 1? • Like THIS? for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i+1]=substr($theline,$i,1); Advantage: I built this code, I understand it, I can make it do whatever I want! 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27 27

  28. One final problem: If you have a string and you need an array ... what to do? example: $theline=”Wombats have no wings”; positions: ”012345678901234567890”; but we want: RECOMMENDATION: Use this kind of code in your project2 to translate one line of your text file into the $letters array. for ($i=0; $i<strlen($theline); $i++) $thecharacters[$i+1]=substr($theline,$i,1); Advantage: I built this code, I understand it, I can make it do whatever I want! 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28 28

  29. Step by Step, building Project 2 • You are of course free to build it in any way you want. • If you need a hint, here are some suggested steps. • Start with "Wheel of Fortune" starter kit • Add a copy of textloader & print out the text (to prove it works) • Add a session variable named $linenumber which recycles (keeping its • same value) except when the NEXT PHRASE button was clicked. • (Print this out, to make sure that NEXT PHRASE is working.) • Now use $linenumber to select a line from the stored text. • like • $line=$mytext[$linenumber]; • Finally, add an error message when there is no more text. 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29 29

More Related