Home

How to Read from the File in PHP for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:14:02
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

If you can write data to a file in PHP, it would make sense that you could read from that file for HTML5 and CSS3 programming as well. The readContact.php program pulls the data saved in the previous program and displays it to the screen.

image0.jpg

It is not difficult to write a program to read a text file. Here's the code:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>readContact.php</title>
</head>
<body>
<h1>Contacts</h1>
<div>
 <?php
 //open up the contact file
 $fp = fopen("contacts.txt", "r") or die("error");
 //print a line at a time
 while (!feof($fp)){
  $line = fgets($fp);
  print "$line <br />";
 }
 //close the file
 fclose($fp);
 ?>
</div>
</body>
</html>

The procedure is similar to writing the file, but it uses a while loop.

  1. Open the file in read mode.

    Open the file just as you do when you write to it, but use the designator to open the file for read mode. Now you can use the fgets() function on the file.

  2. Create a while loop for reading the data.

    Typically, you'll read a file one line at a time. You'll create a while loop to control the action.

  3. Check for the end of the file with.

    You want the loop to continue as long as there are more lines in the file. The feof() function returns the value if you are at the end of the file and false if there are more lines to read. You want to continue as long as feof()returns false.

    The exclamation point (!) operator is a logical not. The condition !feof($fp) is true when there is data left in the file and false when there are no lines left, so this is the appropriate condition to use here.

  4. Read the next line with the fgets() function.

    This function reads the next line from the file and passes that line into a variable (in this case, $line).

  5. Print out the line.

    With the contents of the current line in a variable, you can do whatever you want with it. You could format the contents, search for a particular value, or whatever else you want.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].