Use PHP built-in functionsFgets
You can read a row of content from a file.
Fgets
Syntax:
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.
Feof
A 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,Fgets
The Return Value of the function is a string. If an error occurs, false is returned.