Php the Fgets () function reads a row from the file pointer.
its syntax is:
Fgets (File,length)
Parameters |
Description |
File |
Necessary. Specifies the file to be read. |
Length |
Optional. Specifies the number of bytes to read. The default is 1024 bytes. |
Reads a row from the file pointed to by the files and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (to see the case first). If length is not specified, the default is 1 K, or 1024 bytes.
If it fails, it returns false.
Note: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024 bytes long. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are greater than 8 KB, specifying the length of the largest row in the script is more efficient with resources.
Note: starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not.
Note: You can activate the Auto_detect_line_endings runtime configuration option if you encounter a line terminator for a Macintosh file that is not recognized by PHP when reading a file.
Let's give an example of how to read a file in a row.
Let's say we have a sites.txt file with three lines that reads as follows:
Woyouxian.comblabla.cngoogle.com
The file path for the Sites.txt is:
C:\blabla\php\sites.txt
We use PHP line to read the contents of the file, the PHP code is as follows:
Executing the PHP file, the returned display results are:
Site:woyouxian.comsite:blabla.cnsite:google.com
The first line of the PHP code is to open the file, and the last line is to close a file. The While loop statement indicates that when a file is not finished, it reads a line and loops through it until the file pointer ends at the end of the article.
feof
The function is a built-in function of PHP to test whether the file pointer has reached the end of the file. If it is true, returns FALSE if it is not. The English meaning of EOF is end of file, which is easy to remember.
Normally, fgets
the return value of the function is a string, and if an error occurs, FALSE is returned.