The PHP file reads fread, fgets, fgetc, file_get_contents, and file functions,

Source: Internet
Author: User

The PHP file reads fread, fgets, fgetc, file_get_contents, and file functions,

Fread (), fgets (), fgetc (), file_get_contents (), and file () functions are used to read content from the file.

 

Fread ()

The fread () function is used to read files (which can be safely used for binary files ).
Syntax:

string fread( int handle, int length )

Fread () reads a maximum of length bytes from the file pointer handle. If any of the following conditions is met, the system stops reading files:

  • Maximum length of bytes after reading
  • When the end of the file is reached (EOF)
  • (For Network streams) when a package is available
  • Or (after opening the user space stream) When 8192 bytes have been read

Read 10 bytes (including spaces) from the file ):

<?php// http://www.manongjc.com/article/1346.html$filename = "test.txt";$fh = fopen($filename, "r");echo fread($fh, "10");fclose($fh);?>

Prompt

To read the content of a file into a string, use file_get_contents () with better performance ().

 

Fgets ()

The fgets () function is used to read a row of data from a file and direct the file pointer to the next row.
Tip: Use the fgetss () function if you want to remove the HTML tag from the file during reading.
Syntax:

string fgets( int handle [, int length] )

Fgets () reads a row from the file pointed to by handle and returns a string with a maximum length-1 bytes. When a line break (including the returned value), EOF, or length-1 byte is read, it is stopped. If length is not specified, the default value is 1 K, or 1024 bytes.
Example:

<? Php $ fh = @ fopen ("test.txt", "r") or die ("An error occurred while opening the test.txt file! "); // If condition to avoid Invalid Pointer // http://www.manongjc.com/article/1347.htmlif ($ fh) {while (! Feof ($ fh) {echo fgets ($ fh), '<br/>' ;}} fclose ($ fh);?>

Additional instructions

The feof () function tests whether the file pointer is at the end of the file. The file pointer must be valid. If it is an invalid resource, it will be in an infinite loop. See PHP file pointer Function

 

Fgetc ()

The fgetc () function is used to read the data of a file verbatim until the end of the file.
Syntax:

string fgetc( resource handle )

Example:

<? Php $ fh = @ fopen ("test.txt", "r") or die ("An error occurred while opening the test.txt file! "); // Http://www.manongjc.com/article/1348.htmlif ($ fh) {while (! Feof ($ fh) {echo fgetc ($ fh) ;}} fclose ($ fh);?>

 

File_get_contents ()

The file_get_contents () function is used to read the entire file into a string. A string is returned for success, and FALSE for failure.
Syntax:

string file_get_contents( string filename [, int offset [, int maxlen]] )

Parameter description:
Parameter description
Name of the file to be read by filename
Offset (optional) specifies the start position of the read operation. The default value is the start position of the file.
(Optional) Length of the file to be read, in bytes.
Example:

<? Php // The colleague converts the line break to <br/> echo nl2br(file_get_contents('test.txt ');?>

 

File ()

The file () function is used to read the entire file into an array. Each unit in the array is a corresponding line in the file, including line breaks. An array is returned. If it fails, FALSE is returned.
Syntax:

array file( string filename )

Example:

<? Php $ lines = file('test.txt '); // loop in the array and add the row number // http://www.manongjc.com/article/1349.htmlforeach ($ lines as $ line_num => $ line) {echo "Line # {$ line_num}:", $ line, '<br/>';}?>

Content of the test.txt file:
Hello!
This is the second line of text.
Browser display:
Line #0: Hello!
Line #1: The second Line of text.

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.