PHP file read and write operation of file reading method _php skills

Source: Internet
Author: User
Tags fread function prototype html tags php website readfile
PHP file read operations involve more PHP file manipulation functions than file writes, which are described in detail in code instances.
The way in which data is stored in a text file is mainly three steps and some file operation functions are as follows:
1, open the file (file operation function: fopen)
2, file data read (File operation function: fgets, file, ReadFile, feof, etc.)
3, close the file (file operation function: fclose)

The following is still a PHP file read and write operation code examples to explain the specific application of file reading methods, in the example, by invoking the different PHP file read operation function read the text file data, you can deepen the PHP file read operation function of understanding, in order to PHP Web site development Reasonable application. The data written in the text file comes from the file writing tutorial of PHP file reading and writing, which can also be referred to in the fopen function in relation to the file reading and writing mode.
PHP file read Operation code instance
Copy Code code as follows:

?
$readFun = "Fread";
Switch ($readFun)
{
Case "FGETSS":
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
$allowable _tags = "while (!feof ($fp)) {
$output = Fgetss ($fp, $allowable _tags);
Echo $output;
}
Fclose ($FP);
Break
Case "Fgetcsv":
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
while (!feof ($fp)) {
$output = Fgetcsv ($fp, M, "T");
Print_r ($output);
}
Fclose ($FP);
Break
Case "ReadFile":
echo ReadFile ("Leapsoulcn.txt");
Break
Case "Fpassthru":
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
if (!fpassthru ($FP))
Exit ();
Fclose ($FP);
Break
Case "File":
$output = File ("Leapsoulcn.txt");
Print_r ($output);
Break
Case "FGETC":
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
while (!feof ($fp)) {
$str = fgetc ($FP);
echo ($str = = "\ n"?) <br/> ": $STR);
}
Fclose ($FP);
Break
Case "Fread":
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
Echo fread ($FP, 300);
Fclose ($FP);
Break
Default
@ $fp = fopen ("Leapsoulcn.txt", "R") or Die ("System error");
while (!feof ($fp)) {
$output = fgets ($fp, 100);
Echo $output;
}
Fclose ($FP);
Break
}
?>

Note: In the above example, you can use the $readfun assignment to achieve different PHP file reading method calls, involved in the 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 action function is the function of FGETS,FGETSS and fgetcsv function is the same as fgets, is to read a row in the file, until the end of the file. Here I set the data length in the read text file to be 100, that is, the maximum read length is 99 (100-1), so that the data is stopped when a newline character \ n or a file terminator EOF is encountered or 99 bytes are read from the file. The Fgets function returns the data read by the file, the string type.
The FGETSS function is a variant of the fgets function that can strip PHP and HTML tags and filter unnecessary data by passing the third parameter, which can improve the security of the site, such as filtering user input data in the message book, and FGETSS function prototypes as follows:
Copy Code code as follows:

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

The allowable_tags parameter is optional, in which I have written a line of HTML, body, and H1 tags in the leapsoulcn.txt file in advance, and then in the code I set only allow the H1 tag to appear.
The Fgetcsv function is another variant of fgets, the difference being that, when the data written in your text file uses delimiters, you can use Fgetcsv to break a row into multiple rows, and the returned results are stored in an array with the following function prototypes
Copy Code code 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 delimiters in the file read function fgetcsv in the instance I use \ t and then print out the array structure that fgetcsv returns through Print_r.
Three PHP file read operation functions fgets, FGETSS, fgetcsv in common is the need to use the fopen function in advance 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 after the completion of the read operation to close the file.
FGETC: Reading a single character
The FGETC function is used to read a character, in the code instance I pass a read character, when the \ n character is encountered to convert it to the BR tag in the HTML file, so as to show the specific wrapping effect in the browser, of course, the efficiency of this function is certainly relatively low, not recommended.
The difference between PHP file read operation function ReadFile, fpassthru, file
Three functions in common is that you can read the entire file at once, not one line or one character at a time. The difference is:
The ReadFile function opens the file and returns the contents of the file directly to the viewer, as with the fopen function, the function return value is 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 use the Echo statement not to output the read file content, but the output read the total number of file characters, read the file content ReadFile function has been automatically output, this must be clear! The ReadFile function prototype is as follows:
Copy Code code 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 of one array unit per row. The file function prototype is as follows:
Copy Code code as follows:
Array file (string filename,bool[optional] use_include_path)

The Fpassthru () function is used to output all remaining data at the file pointer, which outputs only the data after the file pointer if the file pointer is not at the beginning. The function reads the given file pointer from the current position to EOF and writes the result to the output buffer, with the returned value of the number of characters output. Returns False when an error occurs. Compared to the ReadFile () function, the Fpassthru () function needs to open the file first, and then close the file when the data is read.
Fread and file_exists, filesize functions
The Fread function is also a way to read a file that reads any byte from the file, either satisfying length or reading to the end of the file. The Read function prototype is as follows:
Copy Code code 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.
Copy Code code as follows:

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

We have not used the file_exists function in the PHP file reading and writing course, usually in the PHP website development, for various considerations, sometimes when the file does not exist, we do not like to create the new file, then we need to use the fopen function before using the File_ The EXISTS function determines whether a file exists, i.e.
Copy Code code as follows:

?
if (file_exists ("Leapsoulcn.txt"))
{
PHP file read and write operation
}
?>

The above is the PHP file read and write operation of the document reading operation of various methods introduced, through the rational application of PHP file read and write operation function, you can achieve a simple message book, Web log and other functions.

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.