Copyright © tutorialspoint.com

PHP Function fgets()

previous
Advertisements

Syntax

string fgets ( resource $handle [, int $length] );

Definition and Usage

Gets a line from file pointer.

Paramters

ParameterDescription
handleThe file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
lengthReading ends when length - 1 bytes have been read, on a newline (which is included in the return value), or on EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line.

Return Value

Returns a string of up to length - 1 bytes read from the file pointed to by handle. If an error occurs, returns FALSE.

Example

Following is the usage of this function:

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (!feof($handle)) {
        $buffer = fgets($handle, 1024);
        echo $buffer;
    }
    fclose($handle);
}
?>
previous

Copyright © tutorialspoint.com