Join Up!
104886 members and counting!

 
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links 
search for in the  
previousfgetcsvfgetssnext
Last updated: Tue, 28 May 2002
view the printer friendly version or the printer friendly version with notes or change language to Czech | German | Spanish

fgets

(PHP 3, PHP 4 >= 4.0.0)

fgets -- Gets line from file pointer

Description

string fgets ( int fp [, int length])

Returns a string of up to length - 1 bytes read from the file pointed to by fp. Reading 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, the length defaults to 1k, or 1024 bytes.

If an error occurs, returns FALSE.

Common Pitfalls:

People used to the 'C' semantics of fgets() should note the difference in how EOF is returned.

The file pointer must be valid, and must point to a file successfully opened by fopen(), popen(), or fsockopen().

A simple example follows:

Esimerkki 1. Reading a file line by line

$fd = fopen ("/tmp/inputfile.txt", "r");
while (!feof ($fd)) {
    $buffer = fgets($fd, 4096);
    echo $buffer;
}
fclose ($fd);

Huomaa: The length parameter became optional in PHP 4.2.0

See also fread(), fopen(), popen(), fgetc(), fsockopen(), and socket_set_timeout().

User Contributed Notes
fgets
add a note about notes
There are no user contributed notes for this page.
previousfgetcsvfgetssnext
Last updated: Tue, 28 May 2002
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by: http://phpbuilder.com/
Last updated: Thu Oct 31 18:34:28 2002 EST