Home> php教程> php手册> body text

How to read files using feof() function in PHP

WBOY
Release: 2016-10-28 15:03:29
Original
1187 people have browsed it

This article mainly introduces the method of using the feof() function to read files in PHP, compares the correct and incorrect usage in the form of examples, and explains the usage skills of the feof() function. Friends in need can refer to it

The example in this article describes how PHP uses the feof() function to read files. Share it with everyone for your reference. The specific usage is as follows:
feof is applied to PHP 4, PHP 5
-used to test whether the file pointer has reached the end of the file.
If the server does not close the connection opened by fsockopen(), feof() will wait until timeout and return TRUE. The default timeout limit is 60 seconds, this value can be changed using stream_set_timeout().
The file pointer must be valid and must point to a file successfully opened by fopen() or fsockopen() (and not yet closed by fclose()).

If the passed file pointer is invalid, it may fall into an infinite loop, because EOF will not return TRUE.
Example #1 feof() using invalid file pointer Example:

php // 如果文件不可读取或者不存在,fopen 函数返回 FALSE $file = @fopen("http://www.manongjc.com/article/1329.html", "r"); // 来自 fopen 的 FALSE 会发出一条警告信息并在这里陷入无限循环.  while (!feof($file)) { } fclose($file); ?>
Copy after login

Example:

php $file = fopen($_SERVER['DOCUMENT_ROOT']."/me/test.txt", "r"); // http://www.manongjc.com/article/1328.html //输出文本中所有的行,直到文件结束为止。  while(! feof($file)) { echo fgets($file). "
"; } fclose($file); ?>
Copy after login

Output:
Hello, this is a test file.
There are three lines here.
This is the last line.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!