Ec (2); php tutorial File Reading operations read data in text files mainly involves three steps and some file operation functions are as follows: 1. Open a file (file operation function: fopen) 2. Read file data (file operation functions: fgets, file, readfile, feof, etc.) 3. close the file (file operation function: fclose) in the Code instance, the default PHP file read operation function is fgets. The fgetss function and fgetcsv function work in script ec (2); script
Php tutorial file read Operations
The Data Storage Method for reading text files involves the following three steps and some file operation functions:
1. open the file (file operation function: fopen)
2. file Data Reading (file operation functions: fgets, file, readfile, feof, etc)
3. close the file (file operation function: fclose)
In code instances, the default PHP file read operation function is fgets. The fgetss and fgetcsv functions are the same as fgets functions. They read one row 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, that is, the maximum read length is 99 (100-1, when the linefeed n, the file Terminator eof, or 99 bytes are read from the file, Data Reading is stopped. The fgets function returns the data read by the file, which is a string type.
The fgetss function is a variant of the fgets function. It can strip php and html tags and pass a third parameter to filter unnecessary data, which improves website security, for example, the message book can filter user input data. The fgetss function prototype is as follows:
The Code is as follows:
String fgetss (resource fp, int length, string [optional] allowable_tags)
Allowable_tags.txt is optional. In this example, a line of text containing the html, body, and h1 mark is written in the leapstutorial oulcn.txt file. Then, in the code, I set that only the h1 mark can appear.
The fgetcsv function is another variant of fgets. The difference is that when the data written to your text file uses a delimiter, you can use fgetcsv to split a row into multiple rows, the returned results are stored in the array. The function prototype is as follows:
The Code is as follows:
Array fgetcsv (resource fp, int length, string [optional] delimiter, string [optional] enclosure)
Delimiter is optional. Because t is used in the data written to the file before, t is used in the file reading function fgetcsv In the instance, then print out the array structure returned by fgetcsv through print_r.
The three PHP file read operation functions fgets, fgetss, and fgetcsv all need to use the fopen function to open the file to be read in advance, and use the feof function to determine whether the file pointer has reached the end of the file, remember to use the fclose function to close the file after the read operation is complete.
Fgetc: read a single character
The fgetc function is used to read one character. In the code example, I read one character at a time and convert it to the br mark in the html file when n characters are encountered, in order to display the specific line feed effect in the browser, of course, the efficiency of this function is certainly relatively low, not recommended.
Differences between the readfile, fpassthru, and file functions for PHP file Reading
In common, the three functions can read the entire file at a time, rather than one row or character at a time. The difference is:
The readfile function opens the file and returns the file content directly to the browser. Like the fopen function, the function returns the total number of characters in the file. The second parameter of the readfile function is optional, specifies whether php should search for files in include_path. In code instances, the echo statement is not used to output the Read File Content, but to output the total number of characters read from the file. The Read File Content readfile function has been automatically output, which must be clear! The readfile function is prototype as follows:
The Code is as follows:
Int readfile (string filename, int [optional] use_include_path)
The file function is another method for reading files. It sends the content of the read files to an array with one array unit per row. The file function is prototype as follows:
The Code is 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 only outputs the data after the file pointer. This function reads the given file pointer from the current position to eof and writes the result to the output buffer. The returned value is the number of output characters. If an error occurs, false is returned. Compared with the readfile () function, the fpassthru () function must first open the file and close the file after the data is read.
Fread, file_exists, and filesize Functions
Fread function is also a method for reading files. It can read any byte from the file, either the length or the end of the file. The read function prototype is as follows:
The Code is as follows: string fread (resource fp, int length)
When you use the fread function, The filesize function can solve this problem when you want to read all the data in the file and do not know the length of the file data. That is
The Code is as follows:
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
Echo fread ($ fp, filesize ("leapsoulcn.txt "));
Fclose ($ fp );
?>
In the php file read/write tutorial, we have not used the file_exists function. Generally, for various considerations, sometimes when the file does not exist, we do not like creating a new file, in this case, we need to use the file_exists function before using the fopen function to determine whether the file exists, that is
The Code is as follows:
If (file_exists ("leapsoulcn.txt "))
{
// Read and write PHP files
}
?>
The above is an introduction to various methods of File Read operations in the PHP File Read/write operation tutorial. By properly applying the PHP file read/write operation function, you can implement simple message books, website log records, and other functions.