How to use PHP function fgets to read pointer files _php tutorial

Source: Internet
Author: User

PHP function Fgets--reading a line from the file pointer
string fgets (int handle [, int length])

Reads a row from the file pointed to by handle and returns a string of up to length-1 bytes in length. Stop after encountering a newline character (included in the return value), EOF, or having read the length-1 byte (see first the case). If length is not specified, the default is 1 K, or 1024 bytes.

Returns FALSE when an error occurs.

PHP functions fgets Common flaws:

People who are accustomed to the fgets () syntax in C should be aware of how EOF is returned.

The file pointer must be valid and must point to a file that was successfully opened by fopen () or Fsockopen ().

Here is a simple example of a PHP function Fgets: Example 1. Read a file line-wise

 
  
  
  1. < ? PHP
  2. $ Handle fopen("/tmp/
    Inputfile.txt "," R ");
  3. while (!feof ($handle)) {
  4. $ Buffer fgets($FD, 4096);
  5. Echo $buffer;
  6. }
  7. Fclose ($handle);
  8. ?>

Note: The length parameter becomes optional from PHP 4.2.0, and if omitted, the line is assumed to be 1024. starting with PHP 4.3, ignoring length will continue to read data from the stream until the end of the line. If most of the rows in the file are larger than 8KB, specifying the length of the largest row in the script is more efficient with resources.
Note: starting with PHP 4.3 This function can be used safely in binary files. The earlier versions were not.

Note: You can activate the Auto_detect_line_endings runtime configuration option if you encounter a PHP function that does not recognize the line terminator of a Macintosh file when reading a file fgets.


http://www.bkjia.com/PHPjc/445958.html www.bkjia.com true http://www.bkjia.com/PHPjc/445958.html techarticle PHP function Fgets--reads a line from a file pointer to a string fgets (int handle [, int length]) reads a row from a file pointed to by handle and returns a maximum length of length-1 bytes ...

  • 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.