How to use PHP built-in function fgets () read pointer file

Source: Internet
Author: User
Tags php language
In the PHP language, there are many powerful functions that support the language's continuous development, and the pie allows more and more programmers to start choosing the language. PHP function fgets is one of the powerful functions.

PHP function Fgets--Reads a line from the file pointer, the syntax is as follows

Fgets (handle, length)

Detailed parameters:

Parameters Description
File Necessary. Specifies the file to be read.
Length Optional. Specifies the number of bytes to read. The default is 1024 bytes.

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

< php  $handle = fopen ("/tmp/test.txt", "R");  while (!feof ($handle)) {  $buffer = fgets ($FD, 4096);  echo $buffer;  }  Fclose ($handle);  ? >

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.

Related Article

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.