PHP File Reading Series method

Source: Internet
Author: User
Tags fread readfile
in the previous PHP tutorial introduced how to use PHP to implement file read and write operations in the file write function, and then share the PHP file read function of the application instance, PHP file read operation relative to the file write operation involves more PHP file operation function, These functions are described in detail in the code instance.

The main three steps involved in reading the data stored in a text file and some of the file operation functions are as follows:

1. Open file (file operation function: fopen)

2, File data reading (File operation function: fgets, file, ReadFile, feof, etc.)

3. Close file (file operation function: fclose)

The following is still the PHP file read and write operation code to explain the specific application of the file reading method, in the instance, by invoking different PHP file read operation function to read the data in the text file, you can deepen the understanding of PHP file read operation function, in the development of PHP Web site reasonable application. The data written in the text file is derived from the file writing tutorial of the PHP file read and write operation, which can also be referenced in the fopen function for file read/write mode.

PHP file read Operation code instance

<?php $readFun = "Fread";            Switch ($readFun) {case ' fgetss ': @ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");            $allowable _tags = "

Notes : In the above example, you can use the $readfun assignment to implement different PHP file read method calls, involving PHP file read operation functions are fgets, FGETSS, Fgetcsv, ReadFile, fpassthru, file, Fgetc and other functions.

In the code instance, the default PHP file read operation function is the function of the FGETS,FGETSS and Fgetcsv functions, like fgets, which reads one line of the file at a time until the end of the file. Here I set the data length in the read text file to 100, which is the maximum read length of 99 (100-1), which stops reading data when a newline character \ n or a file terminator is encountered, or 99 bytes are read from the file. The Fgets function returns the data read by the file, the string type.

FGETC: Reading a single character

FGETC function is used to read a character, and in the code instance I pass a single read character, and when it encounters a \ n character, it is converted to a BR tag in an HTML file to display a specific line-break effect in the viewer, which is certainly inefficient and not recommended.

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: 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.