PHP file Handling-How to read files (one line, whole file)

Source: Internet
Author: User
Tags file handling readfile
php File Handling-How to read files (one line, whole file)

Reading and writing files is more complex than opening and closing files. This is mainly from reading data and writing data two aspects of the explanation.

So in the previous article, "PHP file processing-open and close files" describes the open and close files, open files is the premise of all operations files, and Close is the end of all operations files, then we are today to explain how to read the file!

Reading data from a file

Reading data from a file, you can read a character, a line of strings, or an entire file, and you can read strings of a specified length.

1. Read the entire file : ReadFile (), Flie (), and file_get_contents () three functions, we will introduce each one!

ReadFile () function

The ReadFile () function is used to read a file and write it to the output buffer, which returns False if an error occurs. The function syntax is formatted as follows:

int ReadFile (string $filename [, bool $use _include_path = False [, resource $context]])

Using the ReadFile () function, you do not need to open the file and close the file, do not need to echo, print and other output statements, directly write the file path first.

Flie () function

The Flie () function can also read the contents of the entire file, except that the file () function stores the contents of the files in the array, including line breaks, and returns False if the read fails. The syntax format for 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 contents of the file (Fliename) into a string. Without the offset and MaxLen parameters, a string of length maxlen is read starting at the position specified by the parameter offset. If the read fails then it returns false. The function syntax format is as follows;

String file_get_contents (String $filename [, bool $use _include_path = False [, resource $context [, int $offset =-1 [, int $maxlen]])

This function is suitable for binary files and is the preferred method of reading the contents of the entire file into a string.

The following example reads the contents of a file using the three functions above, with the following code:

The output is:

2. read one row of data: fgets () function and FGETSS () function

(1) fgets () function

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

String fgets (Resource $handle [, int $length])

The parameter handle is the file that is being read, and the parameter length is the amount of data to read. The handle function is capable of reading a row from a specified file and returning a string with a maximum length of length-1 bytes. is to stop with a newline character, EOF, or read length-1 himself. If the length-1 parameter is omitted, the data is read until the line ends.

(2) Fgetss () function

The FGETSS () function is a variant of the fgets () function that reads a row of data. colleague, the FGETSS () function filters out HTML and PHP tags in the content being read. The function syntax is formatted as follows:

String Fgetss (Resource $handle [, int $length [, String $allowable _tags]])

The function can filter out any HTML and PHP tags from the read file, and you can use the Allowable_tags parameter to control which tags are not filtered out.

The following example uses the above function to read a file and display it separately. See what the difference is between them. The specific code is as follows:


The output is:

This article describes two ways to read the file, one is to read the entire file, one is to read a row of data, in the following article I will introduce reading a character and a specified length of the string, read the "PHP file processing-read file (a character, string)"!

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.