Blogroll

photoshop cs6 html 5 css php seo backlinks

adsense

Saturday, 8 February 2014

Reading from a text file PHP free tutorial

In the previous lesson, we learned how to use PHP to access the server's filesystem. In this lesson, we will use that information to read from an ordinary text file.
Text files can be extremely useful for storing various kinds of data. They are not quite as flexible as real databases, but text files typically don't require as much memory. Moreover, text files are a plain and simple format that works on most systems.

Open the text file

We use the documentationfopen function to open a text file. The syntax is as follows:
 fopen(filename, mode)
 
 
filename
Name of the file to be opened.
mode
Mode can be set to "r" (reading), "w" (writing) or "a" (appending). In this lesson, we will only read from a file and, therefore, use "r". In the next lesson, we will learn to write and append text to a file.
The examples in this lesson use the text file unitednations.txt. This is a simple list of the Programmes and Funds of the United Nations and their domains. You can either download the file, or you can create your own file and test the examples with it.
First, let's try to open unitednations.txt:
 <?php

 // Open the text file
 $f = fopen("unitednations.txt", "r");

 // Close the text file
 fclose($f);

 ?>

 
 

Example 1: Read a line from the text file

With the function documentationfgets, we can read a line from the text file. This method reads until the first line break (but not including the line break).
 <html>

 <head>
 <title>Reading from text files</title>
 </head>
 <body>

 <?php

 $f = fopen("unitednations.txt", "r");

 // Read line from the text file and write the contents to the client
 echo fgets($f); 

 fclose($f);

 ?>

 </body>
 </html>
 
 

Example 2: Read all lines from the text file


 <html>

 <head>
 <title>Reading from text files</title>
 </head>
 <body>

 <?php

 $f = fopen("unitednations.txt", "r");

 // Read line by line until end of file
 while(!feof($f)) { 
     echo fgets($f) . "<br />";
 }

 fclose($f);

 ?>

 </body>
 </html>
 
 
In the example, we loop through all the lines and use the function documentationfeof (for end-of-file) to check if you are at the end of the file. If this is not the case ("!" - see lesson 6), the line is written.
Instead of looping through all the lines, we could have achieved the same result with the function fread. If you work with very large text files with thousands of lines, be aware that the fread function uses more resources than the documentationfgets function. For smaller files, it makes very little difference.

Example 3: A simple link directory

As mentioned at the beginning of this lesson, text files can be excellent for data storage. This is illustrated in the next example where we create a simple link directory from the contents of the text file unitednations.txt.
The file is systematically written with the name of the program, then a comma, and then the domain. As you can probably imagine, more information could easily be stored in this comma-separated data file.
To get the information in each line, we use an array.
 <html>
 <head>
 <title>Reading from text files</title>

 </head>
 <body>

 <?php
 $f = fopen("unitednations.txt", "r");

 // Read line by line until end of file
 while (!feof($f)) { 

 // Make an array using comma as delimiter
    $arrM = explode(",",fgets($f)); 

 // Write links (get the data in the array)
    echo "<li><a href='http://" . $arrM[1] . "'>" . $arrM[0]. "</a></li>"; 

 }

 fclose($f);
 ?>

 </body>
 </html>
 
 
Quite handy, right? In principle, you could now just expand the text file with hundreds of links or perhaps expand your directory to also include address information.

0 comments:

Post a Comment