PHP fgets() Function

Usage — The PHP fgets() function is used to get a line from an open file being pointed to by the resource handle. It stops reading after reaching the specified length, encountering a new line or reaching the end of file.

It has the following syntax:

PHP

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

Here is an example of using fgets():

PHP

$file = fopen("story.txt","r");

// Output — ALICE’S ADVENTURES IN WONDERLAND
echo fgets($file);

fclose($file);

Return Value — This function 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.

PHP Version and Changelog — The fgets() function is available in PHP 4, PHP 5, PHP 7. It was made binary-safe in PHP 4.3.0.

Relevant Functions — Other related PHP functions that you should know about are: fgetss() which gets line from file pointer and strip HTML tags, fread() which performs a binary-safe file read, fgetc() which gets character from file pointer, stream_get_line() which gets line from stream resource up to a given delimiter, fopen() which opens file or URL, popen() which opens process file pointer, fsockopen() which opens Internet or Unix domain socket connection and stream_set_timeout() which set timeout period on a stream.

Go Directly To — usage, parameters, working examples or additional tips.

Parameters

handle

The handle parameter is used to specify the file pointer. It must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()). This is a required parameter.

length

The length parameter is used to specify the length of bytes to read. The reading ends when length - 1 bytes have been read, or a newline (which is included in the return value), or an EOF (whichever comes first). If no length is specified, it will keep reading from the stream until it reaches the end of the line. This is an optional parameter. Its default value is 1024 bytes.

You should note that until PHP 4.3.0, omitting it would assume 1024 as the line length. If the majority of the lines in the file are all larger than 8KB, it is more resource efficient for your script to specify the maximum line length.

Working Examples

Here are some examples of using the fgets() function:

PHP

$handle = @fopen("poem.txt", "r");

/* Output —
Twinkle, twinkle, little star
How I wonder what you are
Up above the world so high
Like a diamond in the sky */
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

Further Reading

  1. You can read more about the PHP fgets() function on PHP.net.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top