PHP file reads Fread, fgets, Fgetc, file_get_contents, and the use instance code of the file function

Source: Internet
Author: User
Tags fread

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

Fread ()

The Fread () function is used to read the file (which can be used safely in binary files).

Grammar:

string fread (int handle, int length)

Fread () reads up to length bytes from a file pointer handle. The read file stops when any of the following conditions are encountered:

    • When the maximum length of bytes is read

    • When the end of the file is reached (EOF)

    • (for network streams) when a package is available

    • or (after opening a user-space stream), 8,192 bytes have been read

Read 10 bytes (including spaces) from a file:

<?php$filename = "Data.txt", $fh = fopen ($filename, "R"), Echo fread ($FH, "ten"), Fclose ($FH);? >

Tips

If you just want to read the contents of a file into a string, you should use a better-performing file_get_contents ().

Fgets ()

The fgets () function is used to read a row of data from a file and point the file pointer to the next line.

Tip: Use the FGETSS () function if you want to remove HTML markup from a file while reading it.

Grammar:

string fgets (int handle [, int length])

Fgets () reads a row from the file pointed to by handle and returns a string up to length-1 bytes in length. Stops when a newline character is encountered (including in the return value), EOF, or length-1 bytes have been read. If length is not specified, the default is 1K, or 1024 bytes.

Example:

<?PHP$FH = @fopen ("Data.txt", "R") or Die ("Error opening data.txt file! ");//If condition avoids invalid pointer if ($FH) {while    (!feof ($fh)) {        echo fgets ($fh), ' <br/> ';    }} Fclose ($FH);? >

Additional Information

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

FGETC ()

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

Grammar:

String fgetc (Resource handle)

Example:

<?PHP$FH = @fopen ("Data.txt", "R") or Die ("Error opening data.txt file! "), if ($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, successfully returning a string, and failing to return FALSE.

Grammar:

String file_get_contents (string filename [, int offset [, int maxlen]])
parameter Description:
Parameters Description
FileName The name of the file to read
Offset Optional, specify where the read begins, by default the file start location
MaxLen Optional, specifies the length of the read file, in bytes

Example:

<?php//read when colleagues convert line breaks to <br/>echo nl2br (file_get_contents (' data.txt '));? >

File ()

The file () function is used to read entire files into an array, and each cell in the array is the corresponding line in the file, including newline characters. Returns an array successfully, and FALSE if it fails.

Grammar:

Array file (string filename)

Example:

<?php$lines = File (' data.txt ');//loop in the array and add the line number foreach ($lines as $line _num = $line) {    echo ' line #{$line _num}: ", $line, ' <br/> ';}? >

Test.txt File Contents:

How are you doing! This is the second line of text.

Browser display:

line #0: Hello! Line #1: This is the second row of text. 
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.