Php file processing-how to read a file (one row, the entire file)

Source: Internet
Author: User
Reading and Writing files is more complex than opening and closing files. Here we will explain how to read and write data. Php file processing-how to read a file (one row, the entire file)

Reading and Writing files is more complex than opening and closing files. Here we will explain how to read and write data.

In the previous article "php file processing-open and close files", we introduced how to open and close files. opening a file is the prerequisite for all operating files, and closing is the end of all operating files, we will introduce how to read files today!

Read data from a file

Reading data from a file can read one character, one line of string, or the entire file. It can also read strings of the specified length.

1. read the entire file: Readfile (), flie (), and file_get_contents () functions. we will introduce them one by one!

Readfile () function

The readfile () function is used to read a file and write it to the output buffer. If an error occurs, false is returned. The syntax of the function is as follows:

int readfile ( string $filename [, bool $use_include_path = false [, resource $context ]] )

To use the readfile () function, you do not need to open or close the file, or output statements such as echo or print. you just need to write the file path first.

Flie () function

The flie () function can also read the content of the entire file, but the file () function stores the file content in the array by row, including line breaks. if the reading fails, false is returned. The syntax format of the function is as follows:

array file ( string $filename [, int $flags = 0 [, resource $context ]] )

File_get_contents () function

The file_get_contents () function reads the file content (fliename) into a string. If there is no offset or maxlen parameter, a string with the length of maxlen is read from the position specified by the offset parameter. If the Read fails, false is returned. Function syntax format;

string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )

This function is applicable to binary files and is the preferred method for reading the content of the entire file into a string.

The following example uses the above three functions to read the file content. the specific code is as follows:

 
 
Use the readfile () function to read the file content:
Use the file () function to read the file content: ";}?>
Use the flie_get_contents () function to read the file content:

Output result:

2. read a row of data: fgets () function and fgetss () function

(1) fgets () function

The fgets () function is used to read a row of data at a time. The syntax of the function is as follows:

string fgets ( resource $handle [, int $length ] )

The handle parameter is the read file, and the length parameter is the data length to be read. The function can read a row from the specified handle file and return a string with a maximum length of 1-1 bytes. In the form of a line break, EOF, or read the length-1 and stop. If the length-1 parameter is ignored, the data is read until the row ends.

(2) fgetss () function

The fgetss () function is a variant of the fgets () function, used to read a row of data. Colleague, the fgetss () function filters out the HTML and PHP tags in the read content. The syntax of the function is as follows:

string fgetss ( resource $handle [, int $length [, string $allowable_tags ]] )

This function can filter out any html and PHP tags from the read files. you can use the allowable_tags parameter to control which tags are not filtered out.

The following example uses the above function to read and display a file separately. Observe the differences between them. The code is as follows:

 
 
Use the fgets () function:
Use the fgetss () function:

Output result:

This article introduces two methods to read files. one is to read the entire file and the other is to read a row of data. in the following article, I will introduce how to read a character and a specified length of string, for more information, see php file processing-read a file (one character, string).

The above is the PHP file processing-how to read the details of the file (one row, the entire file), please pay attention to other related articles in the first PHP community!

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.