Handling File Data with PHP

In this fifth part of an eight-part series on working with file and operating systems with PHP, you’ll learn how to read data from a file and move the file pointer. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

Reading from a File

PHP offers numerous methods for reading data from a file, ranging from reading in just one character at a time to reading in the entire file with a single operation. Many of the most useful functions are introduced in this section.

file()

array file (string filename [int use_include_path [, resource context]])

The immensely useful file() function is capable of reading a file into an array, separating each element by the newline character, with the newline still attached to the end of each element. Although simplistic, the importance of this function can’t be understated, and therefore it warrants a simple demonstration. Consider the following sample text file, named users.txt:

Ale ale@example.com
Nicole nicole@example.com
Laura laura@example.com

The following script reads in users.txt and parses and converts the data into a convenient Web-based format:

<?php
$users = file("users.txt");

foreach ($users as $user) {
list($name, $email) = explode(" ", $user);

// Remove newline from $email
$email = trim($email);
echo "<a href="
mailto:$email">$name</a> <br />n";
}
?>

This script produces the following HTML output:

——————————————–<a href="ale@example.com">Ale</a><br />
<a href="nicole@example.com">Nicole</a><br /> <a href="laura@example.com">Laura</a><br />
——————————————–

Like fopen(), you can tell file() to search through the paths specified in the include_path configuration parameter by setting use_include_path to 1. The context parameter refers to a stream context. You’ll learn more about this topic in Chapter 16.

file_get_contents()

string file_get_contents (string filename [, int use_include_path
[resource context]])

The file_get_contents() function reads the contents of filename into a string. By revising the script from the preceding section to use this function instead of file(), you get the following code:

<?php
$userfile= file_get_contents("users.txt");
// Place each line of $userfile into array
$users = explode("n",$userfile);
foreach ($users as $user) {
list($name, $email) = explode(" ", $user);
echo "<a href="mailto:$email">$name/a> <br />";
}
?>

The context parameter refers to a stream context. You’ll learn more about this topic in Chapter 16.

fgetc()

string fgetc (resource handle)

The fgetc() function reads a single character from the open resource stream specified by handle. If the EOF is encountered, a value of FALSE is returned.

fgetcsv()

array fgetcsv (resource handle, int length [, string delimiter
[, string enclosure]])

The convenient fgetcsv() function parses each line of a file specified by handle and delimited by delimiter, placing each field into an array. Reading does not stop on a newline; rather, it stops either when length characters have been read or when the closing enclosure character is located. Therefore, it is always a good idea to choose a number that will certainly surpass the longest line in the file.

Consider a scenario in which weekly newsletter subscriber data is cached to a file for perusal by the corporate marketing staff. Always eager to barrage the IT department with dubious requests, the marketing staff asks that the information also be made available for viewing on the Web. Thankfully, this is easily accomplished with fgetcsv(). The following example parses the already cached file:

<?php
$fh = fopen("/home/www/data/subscribers.csv", "r");
while (list($name, $email, $phone) = fgetcsv($fh, 1024, ",")) {
echo "<p>$name ($email) Tel. $phone</p>";
}
?>

Note that you don’t have to use fgetcsv() to parse such files; the file() and list() functions accomplish the job quite nicely. Reconsidering the preceding example:

<?php
$users = file("users.txt");
foreach ($users as $user) {
list($name, $email, $phone) = explode(",", $user);
echo "<p>$name ($email) Tel. $phone</p>";
}
?>


Note Comma-separated value (CSV) files are commonly used when importing files between applications. Microsoft Excel and Access, MySQL, Oracle, and PostgreSQL are just a few of the applications and databases capable of both importing and exporting CSV data. Additionally, languages such as Perl, Python, and PHP are particularly efficient at parsing delimited data.


fgets()

fgets (resource handle [, int length])

The fgets() function returns either length - 1 bytes from the opened resource referred to by handle, or everything it has read up to the point that a newline or the EOF is encountered. If the optional length parameter is omitted, 1,024 characters is assumed. In most situations, this means that fgets() will encounter a newline character before reading 1,024 characters, thereby returning the next line with each successive call. An example follows:

<?php
$fh = fopen("/home/www/data/users.txt", "rt");
while (!feof($fh)) echo fgets($fh);
fclose($fh);
?>

fgetss()

string fgetss (resource handle, int length [, string allowable_tags])

The fgetss() function operates similarly to fgets(), except that it strips any HTML and PHP tags from handle. If you’d like certain tags to be ignored, include them in the allowable_tags parameter. As an example, consider a scenario in which authors are expected to submit their work in HTML format using a specified subset of HTML tags. Of course, the authors don’t always follow instructions, so the file must be scanned for tag misuse before it can be published. With fgetss(), this is trivial:

<?php
/* Build list of acceptable tags */
$tags = "<h2><h3><p><b><a><img>";

/* Open the article, and read its contents. */
$fh = fopen("article.html", "rt");

while (!feof($fh)) {
$article .= fgetss($fh, 1024, $tags);
}
fclose($fh);

/* Open the file up in write mode
and write $article contents. */
$fh = fopen("article.html", "wt");
fwrite($fh, $article);
fclose($fh);
?>


Tip If you want to remove HTML tags from user input submitted via a form, check out the strip_tags() function, introduced in Chapter 9.


fread()

string fread (resource handle, int length)

The fread() function reads length characters from the resource specified by handle. Reading stops when the EOF is reached or when length characters have been read. Note that, unlike other read functions, newline characters are irrelevant when using fread(); therefore, it’s often convenient to read the entire file in at once using filesize() to determine the number of characters that should be read in:

<?php
$file = "/home/www/data/users.txt";
$fh = fopen($file, "rt");
$userdata = fread($fh, filesize($file));
fclose($fh);
?>

The variable $userdata now contains the contents of the users.txt file.

readfile()

int readfile (string filename [, int use_include_path])

The readfile() function reads an entire file specified by filename and immediately outputs it to the output buffer, returning the number of bytes read. Enabling the optional use_include_path parameter tells PHP to search the paths specified by the include_path configuration parameter. After sanitizing the article discussed in the fgetss() section, it can be output to the browser quite easily using readfile(). This revised example is shown here:

<?php
$file = "/home/www/articles/gilmore.html";

/* Build list of acceptable tags */
$tags = "<h2><h3><p><b><a><img>";

/* Open the article, and read its contents. */
$fh = fopen($file, "rt");

while (!feof($fh))
$article .= fgetss($fh, 1024, $tags);

fclose($fh);

/* Open the article, overwriting it with the sanitized material */
$fh = fopen($file, "wt");
fwrite($fh, $article);
fclose($fh);

/* Output the article to the browser. */
$bytes = readfile($file);
?>

Like many of PHP’s other file I/O functions, remote files can be opened via their URL if the configuration parameter fopen_wrappers is enabled.

fscanf()

mixed fscanf (resource handle, string format [, string var1])

The fscanf() function offers a convenient means for parsing the resource specified by handle in accordance with the format specified by format. Suppose you want to parse the following file consisting of social security (SSN) numbers (socsecurity.txt):

123-45-6789
234-56-7890
345-67-8901

The following example parses the socsecurity.txt file:

<?php
$fh = fopen("socsecurity.txt", "r");

/* Parse each SSN in accordance with
integer-integer-integer format. */

while ($user = fscanf($fh, "%d-%d-%d")) {
list ($part1,$part2,$part3) = $user;

}

fclose($fh);
?>

With each iteration, the variables $part1, $part2, and $part3 are assigned the three components of each SSN, respectively.

{mospagebreak title=Moving the File Pointer}

It’s often useful to jump around within a file, reading from and writing to various locations. Several PHP functions are available for doing just this.

fseek()

int fseek (resource handle, int offset [, int whence])

The fseek() function moves the handle’s pointer to the location specified by offset. If the optional parameter whence is omitted, the position is set offset bytes from the beginning of the file. Otherwise, whence can be set to one of three possible values, which affect the pointer’s position:

  1. SEEK_CUR: Sets the pointer position to the current position plus offset bytes.
  2. SEEK_END: Sets the pointer position to the EOF plus offset bytes. In this case, offset must be set to a negative value.
  3. SEEK_SET: Sets the pointer position to offset bytes. This has the same effect as omitting whence.

ftell()

int ftell (resource handle)

The ftell() function retrieves the current position of the file pointer’s offset within the resource specified by handle.

rewind()

int rewind (resource handle)

The rewind() function moves the file pointer back to the beginning of the resource specified by handle.

Please check back for the continuation of this article.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye