Detailed explanation of how to use PHP built-in function fgets() to read pointer files

怪我咯
Release: 2023-03-13 13:42:01
Original
1823 people have browsed it

In the PHP language, there are many powerful functions that support the continuous development of this language, allowing more and more programmers to choose to use this language. PHP Functionfgets is one of the powerful functions.

PHP function fgets -- Read a line of instructions from the file pointer, the syntax is as follows

 fgets ( handle ,length )
Copy after login

Detailed explanation of parameters:

##ParametersDescriptionfileRequired. Specifies the file to be read. length Optional. Specifies the number of bytes to read. The default is 1024 bytes.
Read a line from the file pointed to by handle and return a

string of length at most length - 1 bytes. Stops when a newline character (included in the return value), EOF, or length - 1 bytes has been read (whichever occurs first). If length is not specified, it defaults to 1K, or 1024 bytes.

Return FALSE on error.

Common defects of PHP function fgets:

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

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

The following is a simple example of the PHP function fgets: Example 1. Read the file line by line

##

< ?php  
$handle = fopen("/tmp/test.txt", "r");  
while (!feof($handle)) {  
$buffer = fgets($fd, 4096);  
echo $buffer;  
}  
fclose($handle);  
?>
Copy after login
Note: The length parameter becomes optional starting from PHP 4.2.0. If omitted, the row length is assumed to be 1024. Starting with PHP 4.3, omitting length will continue reading from the stream until the end of the line. If most of the lines in the file are larger than 8KB, specifying the maximum line length in the script is more efficient in utilizing resources. Note: Starting from PHP 4.3, this function can be safely

used for binary files. Earlier versions did not.
Note: If you encounter that the PHP function fgets cannot recognize the line ending characters of Macintosh files when reading files, you can activate the auto_detect_line_endings runtime configuration option.

The above is the detailed content of Detailed explanation of how to use PHP built-in function fgets() to read pointer files. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!