PHP fgets() Function
In this chapter you will learn:
- Definition for PHP fgets() Function
- Syntax for PHP fgets() Function
- Parameter for PHP fgets() Function
- Return for PHP fgets() Function
- Example - reads a large log line by line
Definition
fgets() is good for reading large files line by line. It sends back the next line as its return value.
Syntax
string fgets ( resource $handle [, int $length ] )
Parameter
- handle - The file pointer opened by fopen() or fsockopen().
- length - Reading ends when length - 1 bytes have been read, or a newline, or an EOF. If no length is specified, it will keep reading from the stream until it reaches the end of the line.
Return
Returns a string of up to length - 1 bytes read from the file pointed to by handle. If there is no more data to read in the file pointer, then FALSE is returned.
If an error occurs, FALSE is returned.
Example
Reads a large log line by line:
<?PHP/*from j a va 2 s.co m*/
$access_log = fopen("access_log", "r");
while (!feof($access_log)) {
$line = fgets($access_log);
print $line;
print "\n";
}
fclose($access_log);
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP fgetss() Function
- Syntax for PHP fgetss() Function
- Parameter for PHP fgetss() Function
- Return for PHP fgetss() Function
- Example - Read a html file
Home » PHP Tutorial » PHP File Functions