Displaying files - Page 2
July 22, 2002
If you simply want PHP to display the contents of a file in the
browser window, you can use either the fpassthru() or
readfile() functions. Both are exceptions to the usual
rule, where any work with files requires both an fopen()
and fclose() function call. The readfile() function
requires neither - it takes the contents of a text file, and
outputs to the standard output, (which is normally the browser
if called from a PHP web page). The function returns either the
number of bytes (if successful), or false (if the read failed).
The syntax is:
int readfile (string filename [, int use_include_path])
<?
$filename = "c:\\includes\\page.html";
if (!(readfile($filename))) {
print "Cannot display the file: $filename ";
}
?>
If the file is not a text file (or even if it is), you can use
the fpassthru() function instead. The only difference to
readfile() is that fpassthru() requires the file handle (so the
file must be opened already, although it does automatically close
it), and that it can read binary files. Remember to use the 'b'
flag if the file is binary, such as an image.
<?
$filename = "c:\\images\\lion.gif";
if (!($fp = fopen ($filename, "rb"))) {
print "Error - could not open the file $filename";
}
else {
fpassthru($fp);
}
?>
Reading from files
Of course displaying the contents of a file in the browser
window is not the only thing you'd want to do. More often, files
contain other information that needs to be processed in some way.
To use the contents of the file in your PHP script, you'll need
to read the file, and place the contents of the file into PHP
variables. There are a number of functions that help with this.
The file() function reads the contents of a file line by
line, returning an array, each element of the array corresponding
to one line. Each line will still have the linebreak at the end.
This function is another exception to the rule of having to open
and close files. The syntax and an example follow.
array file ( string filename [, int use_include_path])
<?
// place the results of a file into the $filearray array
$filename = "/home/ian/hiddensecrets.txt";
if (!($filearray = file ($filename))) {
print "Can't open file $filename";
}
else {
while (list ($line_number, $line_contents) = each ($filearray)) {
// do something with $line_contents...
}
}
?>
The file() function has a number of problems. For one, it
is not binary safe (meaning it cannot be used safely on binary
files). It assumes the file is text, and each line ends in a line
break. Also, since the entire file is loaded into memory, be very
careful that you have enough memory to take the contents! If the
file size is out of your control, for security reasons I suggest
you use one of the methods discussed after this. And finally,
reflecting PHP's Unix roots, earlier versions of PHP4 had
problems with non-Unix line breaks.
A binary safe function that can be used to read the contents of
a file is fgets(). This reads the contents of a file until
it reaches the end of the line, or until the length specified. A
related function that is used often in this situation is the
feof() function, which checks whether the end of the file
has been reached. It returns TRUE if the end of the file
is reached (or there's an error), and false otherwise, so it's
perfect for using in a while loop to iterate through the contents
of a file. The syntax of both functions, and an example follow :
string fgets ( int fp [, int length])
int feof ( int fp)
<?
$filename = "/home/ian/hiddensecrets.txt";
if (!($fp = fopen ($filename, "r"))) {
print "Error - could not open the file $filename";
}
else {
while (!feof ($fp)) {
$line_contents = fgets($fp, 4096);
// do something with $line_contents...
}
fclose ($fp);
}
?>
From PHP 4.2.0, the length parameter became optional. Including a
length parameter leads to the possibility of the line being longer
than the length parameter, (specified in bytes), meaning that the
line could possibly have been cut off. Leaving the length
parameter out avoids this possibility. The downside again is the
possibility of trying to load too much into memory. I suggest
leaving the length parameter for security reasons in in most
cases, as you'll usually know the maximum length of the line you'll be processing.
Handling files with PHP4 - Part1
Handling files with PHP4 - Part1
Displaying files (Cont.) - Page 3
|