Working with text files - Reading from a text file
(Page 5 of 10 )
The most common way of reading from a text file is to use the fread() function though there are many other ways to read as well. We will discuss about other functions in a later chapter of this tutorial. To get a reasonable understanding of this part, we'll talk about fread() here.
To read a file, there should be a variable with a pointer to a opened file which we mentioned in last few chapters. This is the common syntax of this function.
<?php $content = fread( $fp, filesize( $filename ) ); ?> |
The first parameter is, as usual, a pointer to a file. Second parameter is the number of bytes that should be read in by the function. Usually, when we need to read the whole file, we use the filesize() function to help this matter. It will return the total bytes of the given file. So, then fread() gets its second parameter as total number of bytes. It places the content of the whole file pointed by $fp in the $content variable. You may use $content to do any type of processing as now it has the all ingredients of the file.
For more help: http://us2.php.net/manual/en/function.fread.php
Next: Deleting a file >>
More Programming Basics Articles
More By Codewalkers