fgets
Read a line from a file pointer
The fgets()
function reads a line from a file pointer.
<?php $file = fopen("test.txt","r"); echo fgets($file); fclose($file); ?>
输出类似:
Hello, this is a test file.
<?php $file = fopen("test.txt","r"); while(! feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?>
输出类似:
Hello, this is a test file. There are three lines here. This is the last line.
fgets ( file , length )
parameter | describe |
---|---|
file | Required. Specifies the file to be read. |
length | Optional. Specifies the number of bytes to be read. The default is 1024 bytes. |
Read a line from the file pointed to by the file and return a string of length up to length - 1 byte. It encounters a newline character (including in the return value), EOF, or stops after length - 1 byte has been read (it depends on which case it encounters first). If length is not specified, the default is 1K, or 1024 bytes.
If it fails, return false.