Application of PHP file operation function function

Source: Internet
Author: User
Tags fread php web development readfile
This article mainly introduces the PHP file read function application examples and commonly used file data read function of the use of methods and differences, very detailed, the need for small partners can refer to the next.

PHP file read operations involve more PHP file manipulation functions relative to file writes, which are described in detail in code examples.

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

< $readFun = "fread"; switch ($readFun) {case "FGETSS": @ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error"); $ Allowable_tags = "

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

PHP file read operation function fgets, FGETSS, fgetcsv the difference between

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.

FGETSS function is a variant of the Fgets function, it can peel PHP and HTML tags, by passing the third parameter to filter unnecessary data, can improve the security of the site, such as the message can filter the user's input data, the FGETSS function prototype is as follows:

String Fgetss (Resource fp,int length, string[optional] allowable_tags)

The allowable_tags parameter is optional, in which I have previously written a line of text containing HTML, body, H1 tags in the leapsoulcn.txt file, and then in the code I set only allow H1 tags to appear.

The Fgetcsv function is another variant of fgets, and the difference is that when you write data in your text file using a delimiter, you can use Fgetcsv to break a row into multiple rows, the returned results are stored in an array, and the function is prototyped as follows:

Array Fgetcsv (Resource fp,int length, string[optional] delimiter,string[optional] enclosure)

Delimiter is optional because I used \ t in the data previously written to the file, so the delimiter in the file read function fgetcsv in the instance I used \ t, and then I printed out the array structure returned by fgetcsv through Print_r.

Three PHP file read operation function fgets, FGETSS, fgetcsv in common is the need to use the fopen function to open the read file, while the feof function to determine whether the file pointer to the end of the file, remember to use the Fclose function to close the file after the read operation is complete.

FGETC: Reading a single character
The 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.

PHP file read operation function ReadFile, fpassthru, file the difference between

Three functions in common is the ability to read the entire file at a time, rather than one line or character at a time. The difference is:
The ReadFile function opens the file, returns the contents of the file directly on the viewer, and, like the fopen function, the function returns the total number of characters in the file, and the second parameter of the ReadFile function is optional, indicating whether PHP should find the file in Include_path. In the code instance, I used the Echo statement not to output the read file content, but to output the total number of file characters read, and read the contents of the file ReadFile function has been automatically output, this must be clear! The ReadFile function is prototyped as follows:
int ReadFile (string filename,int[optional] use_include_path)

The file function is another way to read files, which is to send the contents of the read file to an array, one array unit per line. The file function is prototyped as follows:
Array file (string filename,bool[optional] use_include_path)

The Fpassthru () function is used to output all the remaining data at the file pointer, that is, if the file pointer is not at the beginning, it outputs only the data that follows the file pointer. The function reads the given file pointer from the current position to EOF and writes the result to the output buffer, returning the value as the number of characters in the output. Returns False when an error occurs. Compared to the ReadFile () function, the Fpassthru () function needs to open the file first and close the file after the data has been read.

Fread and file_exists, filesize functions

The Fread function is also a way to read a file, which can read any byte from a file, either satisfying length or reading to the end of the file. The read function is prototyped as follows:

String Fread (Resource fp,int length)

When you use the Fread function, the FileSize function solves the problem when you want to read all the data in the file without knowing the length of the file data.

<?  @ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");  Echo fread ($fp, FileSize ("Leapsoulcn.txt")); Fclose ($FP);? >

In the php file read and write operation tutorial we have not used the file_exists function, usually in PHP web development, for various reasons, sometimes when the file does not exist, we do not like to create a new file, then we need to use the fopen function before using File_ The EXISTS function determines whether a file exists, i.e.

<?if (file_exists ("Leapsoulcn.txt")) {//for PHP file read and write operations}?>

Summary : The above is the entire content of this article, I hope to be able to help you learn.

Related recommendations:

How PHP automatically generates a form

How PHP operates the MySQL database

Php method for server-side pre-defined variable $_server

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.