Read a file line with PHP built-in function Fgets-Krebs

Source: Internet
Author: User
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.

feofThe 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.