Using the feof () function to read files in PHP

Source: Internet
Author: User
This article mainly introduces how to use the feof () function to read files in PHP, compares the correct and incorrect usage in the form of examples, and clarifies the usage skills of the feof () function, for more information, see

This article mainly introduces how to use the feof () function to read files in PHP, compares the correct and incorrect usage in the form of examples, and clarifies the usage skills of the feof () function, for more information, see

This example describes how to use the feof () function to read files in PHP. Share it with you for your reference. The usage is as follows:

Feof applies to PHP 4 and PHP 5
-It is 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 () waits until it times out and returns TRUE. The default timeout limit is 60 seconds. You can use stream_set_timeout () to change this value.

The file pointer must be valid and must be directed to a file successfully opened by fopen () or fsockopen () (not closed by fclose ).

If the passed file pointer is invalid, it may be in an infinite loop, because EOF does not return TRUE.
Example #1 Example of using feof () with invalid file pointer:

The Code is as follows:

<? Php
// If the file cannot be read or does not exist, the fopen function returns FALSE.
$ File = @ fopen ("no_such_file", "r ");

// FALSE from fopen generates a warning message and falls into an infinite loop here
While (! Feof ($ file )){
}
Fclose ($ file );
?>



Example:

The Code is as follows:

<? Php
$ File = fopen ($ _ SERVER ['document _ root']. "/me/test.txt", "r ");

// Output all rows in the text until the end of the file.
While (! Feof ($ file ))
{
Echo fgets ($ file )."
";
}
Fclose ($ file );
?>

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

I hope this article will help you with PHP programming.

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.