PHP file read/write operations

Source: Internet
Author: User
Php file read/write operations. Php file read operations involve more php file operation functions than file write operations. these functions are described in detail in the code instance. Reading text files to store data mainly involves more php file operation functions than file write operations. these functions are described in detail in the code instance.
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)

The following uses the php file read/write operation code instance to explain the specific application of the file read method. in the instance, you can call different php file read operation functions to read data in the text file, you can learn more about the function for reading PHP files so that they can be used properly in PHP website development. The data written to a text file comes from the PHP file read/write operation file writing tutorial. for details about the file read/write mode in the fopen function, refer to this article.
Php file read operation code instance

The code is as follows:


$ ReadFun = "fread ";
Switch ($ readFun)
{
Case "fgetss ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
$ Allowable_tags = "";
While (! Feof ($ fp )){
$ Outputs = fgetss ($ fp, 100, $ allowable_tags );
Echo $ output;
}
Fclose ($ fp );
Break;
Case "fgetcsv ":
@ $ Fp = fopen ("leapsoulcn.txt", "r") or die ("system error ");
While (! Feof ($ fp )){
$ Output = fgetcsv ($ fp, 100, "\ 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 "? "
": $ 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 assign a value to $ readFun to call different methods for reading php files, the involved php file read operation functions include fgets, fgetss, fgetcsv, readfile, fpassthru, file, and fgetc.
Differences between functions fgets, fgetss, and fgetcsv for PHP file reading
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 or 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 leapsoulcn.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 I used \ t in the data written to the file, I used \ t in the fgetcsv function for file reading 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 a character. in the code example, I read the character one by one. when the \ n character is encountered, it is converted into the br Mark in the html file, 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.

Bytes. Reading text files to store data mainly...

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