Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

// open file for reading
$myFile = fopen("data.txt","r");


// make sure the open was successful
if(!($myFile))
{
print("file could not be opened");
exit;
}


while(!feof($myFile))
{
// read a line from the file
$myLine = fgets($myFile, 255);
print("$myLine
\n");
}


// close the file
fclose($myFile);
?>


boolean feof(integer file_handle)


As you read from a file, PHP keeps a pointer to the last place in the file you read. The
feof function returns TRUE if you are at the end of the file. It is most often used in the
conditional part of a while loop where a file is being read from start to finish. See the
description of fclose, above, for an example of use. If you need to know the exact
position you are reading from, use the ftell function.


string fgetc(integer file_handle)


The fgetc function returns a single character from a file. It expects a file handle as
returned by fopen, fsockopen, or popen. Some other functions for reading from a file
are: fgetcsv, fgets, fgetss, fread, gzgetc.


<?
// open file and print each character
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}


fclose($myFile);
}
?>

Free download pdf