Reading the File
We will now enhance the prior example, providing a means to read the contents of a file after writing to it. Reading from a file requires the use of the fread() function:
string fread (resource handle, int length)
The fread() function reads up to length bytes from the file pointer referenced by handle. Reading stops when length bytes have been read, EOF (end of file) is reached, or (for network streams) when a packet becomes available, whichever comes first.
$file = "newfile.txt";
if (!$file_handle = fopen($file,"r")) {
echo "Cannot open file.";
}
if (!$file_contents = fread($file_handle, filesize($file))) {
echo "Cannot retrieve file contents.";
}
else { echo "$file_contents"; }
fclose($file_handle);
?>
Tuesday, April 17, 2007
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment