PHP - Function fgets()



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

Definition and Usage

Gets a line from file pointer.


Parameter Description
handle The file pointer must be valid, and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).
length 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, 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.


Following is the usage of this function −

   $handle = @fopen("/tmp/inputfile.txt", "r");
   if ($handle) {
      while (!feof($handle)) {
         $buffer = fgets($handle, 1024);
         echo $buffer;