Introduction to PHP feof () functions and methods for reading files using the feof () function

Source: Internet
Author: User
This article mainly introduces PHP using the feof () function read the file method, with instance form contrast correct and wrong usage, clarified the use of feof () function skill, need friends can refer to the next

The feof () function detects if the end of file (EOF) has been reached. Returns an error (including the socket timeout) if the file pointer goes to EOF, or TRUE If an error occurs, otherwise returns FALSE.

Feof applied to PHP 4, PHP 5
-Used to test whether the file pointer is at the end of the file.

If the server does not close the connection opened by Fsockopen (), feof () waits until the timeout expires and returns true. The default time-out limit is 60 seconds, and you can use Stream_set_timeout () to change the value.

The file pointer must be valid and must point to a file that was 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 does not return TRUE.
Example #1 the feof () example using invalid file pointers:

The code is as follows:

<?php//If the file is unreadable or does not exist, the fopen function returns False$file = @fopen ("No_such_file", "R");//FALSE from fopen emits a warning message and is trapped here in an infinite loop while (!feof ($file)) {}fclose ($file);? >

Example: The code is as follows:

<?php  $file = fopen ($_server[' document_root '). " /me/test.txt "," R ");    Prints all the lines in the text until the end of the file. While  (! feof ($file))  {    echo fgets ($file). "<br/>";  }  Fclose ($file);  ? >

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

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.