Displaying files (Cont.) - Page 3
July 22, 2002
A similar function to fgets(), but one which strips all
HTML and PHP tags from the line being read, is fgetss.
It takes a parameter, allowable_tags which caters for
exceptions, or tags that are not to be stripped.
string fgetss ( int fp, int length [, string allowable_tags])
<?
$filename = "/home/ian/potofgold.html";
if (!($fp = fopen ($filename, "r"))) {
print "Error - could not open the file $filename";
}
else {
while (!feof ($fp)) {
$line_contents = fgetss($fp, 4096);
// do something with $line_contents...
}
fclose ($fp);
}
?>
Be careful, as only tags from the line returned are stripped. If
the tag spans a line, it will not be removed.
Instead of reading the entire file, or one line at a time, you
can read one character at a time with the fgetc() function.
Each time it is called, the file pointer moves along one
character, so the next operation that uses the file pointer will
start at that point.
string fgetc ( int fp)
<?
$filename = "/home/ian/potofgold.txt";
if (!($fp = fopen ($filename, "r"))) {
print "Error - could not open the file $filename";
}
else {
while (!feof ($fp)) {
$file_char = fgetc($fp);
// do something with $file_char...
}
fclose ($fp);
}
?>
The above functions may not be enough - in some cases you want
complete control. The fread() function allows you to read
any number of bytes you specify.
string fread ( int fp, int length)
To read the first ten bytes of a file, you could use the following:
<?
$filename = "/home/ian/goodThingsComeInTens.txt";
if (!($fp = fopen ($filename, "r"))) {
print "Error - could not open the file $filename";
}
else {
$first_ten = fread ($fp,10);
fclose ($fp);
}
?>
A useful function that is often used in conjunction with
fread() is filesize(). This function does not
require a file handle as it takes the file name as a parameter
(so it does not require the fopen() function to be called
first). It's used with fread() when you want to read the entire
contents of the file into memory, (but remember to be careful not
to read too much!)
int filesize ( string filename)
<?
$filename = "/home/ian/goodThingsComeInTens.txt";
if (!($fp = fopen ($filename, "r"))) {
print "Error - could not open the file $filename";
}
else {
$first_ten = fread ($fp,filesize($filename));
fclose ($fp);
}
?>
Note that filesize() cannot be used on remote files, only those
on the local system.
Writing to files
Writing to a file is simply a matter of opening the file for
writing, (whether it's appending at the end of the file with an
'a' mode or overwriting the file with a 'w' mode), and passing
what you want written to either the fputs() or
fwrite() function. These two functions are identical
int fwrite ( int fp, string string [, int length])
int fputs ( int fp, string str [, int length])
<?
$filename = "/home/ian/blankcheque.txt";
if (!($fp = fopen ($filename, "w"))) {
print "Error - could not open the file $filename";
}
else {
$content1 = 'Start of the file';
fputs($fp,$content1);
$content2 = 'End of the file';
fputs($fp,$content2);
fclose ($fp);
}
?>
The integer returned can be used for error checking, as it
returns the number of characters written, or -1 if the write
operation failed. The optional length parameter can be used to
limit the number of characters written. If the string being
written exceeds the length, only the length number of characters
will be written. In general, when writing binary data, you should
always specify the length.
If you want to write data onto separate lines of the file, you'll
need to add a line break at the end of the line. Line breaks are
different in Windows and in Unix. A Unix linebreak is simply a
newline character ('\n'), while a Windows linebreak is a carriage
return and a newline character ('\r\n').
<?
$filename = "/home/ian/logfile.txt";
if (!($fp = fopen ($filename, "a"))) {
print "Error - could not open the file $filename";
}
else {
$content1 = 'first log entry\n'; // or \r\n in Windows
fputs($fp,$content1);
$content2 = 'second log entry\n'; // or \r\n in Windows
fputs($fp,$content2);
fclose ($fp);
}
?>
There's a lot more to this topic, and we'll explore some more in
the next part of this article. Part 2 will cover moving the
current position in the file, copying, and deleting files,
getting more information about the file, as well as uploading
files.
Resources
Download the scripts from this article
Official PHP site
Displaying files - Page 2
Handling files with PHP4 - Part1
Handling files with PHP4 - Part 2 - Page 4
|