How to use the PHP function fgets to read pointer files _ PHP Tutorial

Source: Internet
Author: User
Use the PHP function fgets to read pointer files. PHP function fgets -- read a line from the file pointer. stringfgets (inthandle [, intlength]) reads a line from the file pointed to by handle and returns a line with a maximum length of-1 bytes.

PHP function fgets -- read a line of description from the file pointer
String fgets (int handle [, int length])

Read a row from the file pointed to by handle and return a string of up to length-1 bytes. When a line break (including the returned value), EOF, or the length-1 byte is read, it is stopped ). If length is not specified, the default value is 1 K, or 1024 bytes.

If an error occurs, FALSE is returned.

Common defects of PHP function fgets:

People who are used to the fgets () syntax in C should notice how EOF is returned.

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

Below is a simple example of a PHP function fgets: Example 1. read files row by row

 
 
  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 is optional since PHP 4.2.0. If this parameter is ignored, the row length is assumed to be 1024. Starting from PHP 4.3, ignoring length will continue to read data from the stream until the row ends. If most of the rows in the file are larger than 8 kB, it is more effective to specify the length of the maximum row in the script to use resources.
Note: Starting from PHP 4.3, this function can be safely used for binary files. Earlier versions do not work.

Note: If the PHP function fgets does not recognize the row Terminator of the Macintosh File when reading the file, you can activate the auto_detect_line_endings runtime configuration option.


Delimiter -- read a line from the file pointer indicating string fgets (int handle [, int length]) read a row from the file pointed to by handle and return a maximum 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.