PHP fgets() Function

Definition and Usage

The fgets() function reads a line from a file pointer.

Syntax

fgets(from,The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring)
Parameter Description
from Required. Specifies the file to read.
The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring Optional. Specifies the number of bytes to read. The default is 1024 bytes.

说明

Description from file The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring Points to a file and returns a string containing up to The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring - 1-byte string. Hits the newline character (including in the return value), EOF, or has already read The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring- Stops after 1 byte (depending on which one comes first). If not specified

Then the default is 1K, or 1024 bytes.

If it fails, it returns false.

From PHP 4.3, this function can be used safely with binary files. The earlier versions cannot.The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring Tips and Notes The parameter has become optional since PHP 4.2.0. If ignored, the line length is assumed to be 1024 bytes. From PHP 4.3, ignoring length

From PHP 4.3, this function can be used safely with binary files. The earlier versions cannot.It will continue to read data from the stream until the end of the line. If most of the lines in the file are greater than 8 KB, it is more effective to specify the maximum line length in the script in terms of resource usage.

From PHP 4.3, this function can be used safely with binary files. The earlier versions cannot.Notes:

If PHP cannot recognize the line ending of a Macintosh file when reading the file, you can activate the auto_detect_line_endings runtime configuration option.

Instance

<?php
$file = fopen("test.txt","r");
Example 1
fclose($file);
?>

Output similar to:

Hello, this is a test file.

Example 2

<?php
$file = fopen("test.txt","r");
while(! feof($file))
  {
  echo fgets($file) . "<br />";
  }
fclose($file);
?>

Output similar to:

Hello, this is a test file. 
There are three lines here. 
This is the last line.