Use the PHP built-in function fgets to read a row of content in the file

Source: Internet
Author: User

Use PHP built-in functionsFgetsYou can read a row of content from a file.

FgetsSyntax:

 
Fgets (Filepointer)

The following example shows how to read a file in one row.

Suppose we have a sites.txt file, which has three lines and the content is as follows:

 
Woyouxian.com
Blabla.cn
Google.com

The file path of sites.txt is:

 
C: \ blabla \ PHP \ sites.txt

We use PHP to read the file content in one row.CodeAs follows:

 
<HTML>
<Body>
<? PHP
$ F = fopen ("C: \ blabla \ PHP \ sites.txt", "R ");
While (! Feof ($ F ))
{
$ Line = fgets ($ F );
Echo "site:", $ line, "<br/> ";
}
Fclose ($ F );
?>
</Body>
</Html>

Run the PHP file and the returned result is:

 
Site: woyouxian.com
Site: blabla.cn
Site: 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 in indicates that when the file is not finished, a row is read and executed cyclically until the file pointer reachesArticleEnd.

FeofA function is a built-in function in PHP to test whether the file pointer has reached the end of the file. If true is returned, if not, false is returned. The English meaning of EOF is end of file, which is easy to remember.

Under normal circumstances,FgetsThe Return Value of the function is a string. If an error occurs, false is returned.

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.