PHP Basics PHP file operations-file reading

Source: Internet
Author: User
Tags php basics readfile
There are many ways to read a file in PHP. reading a file is more flexible and convenient than opening a file and writing a file. PHP provides several functions for developers to choose from in actual project development. let's take a look at these functions and their usage.

There are many ways to read a file in PHP. reading a file is more flexible and convenient than opening a file and writing a file. PHP provides several functions for developers to choose from in actual project development. let's take a look at these functions and their usage.

1. know when to read the file function: feof ()

First, let's take a look at the example, define a variable volume, use the open file extension fopen(folder to open the file File.txt in read-only mode, and then use the while loop to read the file content until the end of the file. In this while loop statement, use the feof () function as the test condition for the end of the file:
$ Fp = fopen ("file.txt", 'RB ');
While (! Feof ($ fp ));

The unique parameter of the function feof () is the file pointer. If the file pointer points to the end of the file, it returns true. Although this function name looks a bit odd, it is easy to remember if feof indicates File End Of File (from the beginning to the End Of the File. In this example, we continue to operate on the file until we encounter an EOF.

2. functions for reading a row of data: fgets (), fgetss (), and fgetcsv ()

The following example uses the fgets () function to read file content:
$ Rfile = fgets ($ fp, 100 );

This function can read a row of content from the file each time. In this way, it will continue to read data until it reads a newline character (n), the file terminator EOF, or 99 B from the file. The maximum length that can be read is the specified length minus 1B. You can also use other functions to read files. When you need to process some plain text files in block mode, the fgets () function will be very useful. An interesting variant of the fgets () function is the fgetss () function. its function structure is as follows:
String fgetss (resource fp, int length, string [allowable_tags]);

The fgetss () function is very similar to fgets (), but it can filter PHP and HTML tags contained in strings. To filter out any special tags, you can include them in the allowable_tags string. When reading files written by others or files containing user input, you can use the fgetss () function for operation security considerations. Allowing unrestricted HTML code to appear in a file may corrupt your well-designed format. Allowing unrestricted PHP code to appear in a file may allow malicious users to control the server in an almost free way.

The fgetcsv () function is another variant of fgets. It has the following function prototype:
Array fgetcsv (resource fp, int length [, string delimiter
[, String enclosure])

Example: $ rfile = fgetcsv ($ fp, 100, "t ");

The above code reads a row from the file and splits the file content into different lines where there is a tab (t. This function returns an array. The length parameter must be greater than the number of characters in the longest data row in the file to be read. The enclosure parameter is used to specify characters that close each domain in each line. If no character is specified, this character is "(double quotation marks) by default ).

3. functions for reading the entire file: readfile (), fpassthre (), and file ()

In addition to reading a single row of files, you can also read the entire file at a time. PHP provides four different methods to read the entire file.

The first method is readfile (). You can use the following statement to replace all the scripts fully written: readfile ("file.txt ");. Call the readfile () function to open the file, output the file content to the standard output (WEB browser), and then close the file. The basic structure of the readfile () function is as follows:
Int readfile (string filename, [int use_include_path [, resource context]);
The second optional parameter specifies whether PHP should search for files in include_path, which is the same as the fopen () function. The optional context parameter is used only when the file is remotely opened. The return value of this function is the total number of bytes read from the file.

The second method is fpassthru (). To use this function, you must first open the file with fopen. Then pass the file pointer as a parameter to fpassthru (). In this way, the file content pointed to by the file pointer can be sent to the standard output. Close the file. Example:
$ Fp = fopen ("file.txt", 'RB ');
Fpassthru ($ fp );

If the operation succeeds, the fpassthru () function returns true; otherwise, false.

The function used to read the entire file is file (). In addition to the ability to display the file content back to the standard output, it is the same as readfile (), which sends the results to an array. You can call the file () function in this way: $ filearray = file ("file.txt ");. This line of code can read the entire file into an array named $ filearray. Each row in the file is saved as an element in this array. Note that in earlier versions of PHP, this function is not safe for binary.

The fourth option is to use the file_get_content () function. This function is the same as the readfile () function, but it returns the file content in the form of a string instead of directly returning the file content to the browser.

4. read a character function: fgetc ()

Another way to read a file is to read one character from one file at a time. You can use the fgetc () function. It has a file pointer parameter, which is also the unique parameter of the function, and it will return the next character of the file. We can use a loop with the fgetc () function to replace the while loop in the original script, as shown below:
While (! Feof ($ fp )){
$ Char = fgetc ($ fp );
If (! Feof ($ fp ))
Echo ($ char = "n ""? "
": $ Char );
}

This code uses the fgetc () function to read a character from the file and save the character in $ char until the end of the file. And then use the HTML line break (
) To replace the line terminator (n) in the text ). One disadvantage of using the fgtec () function is that EOF is returned, while fgets () does not. After reading the characters, you also need to judge feof (), because we do not want to echo the file terminator EOF to the browser. If you do not need to process files one by one for some reason, this method of reading characters one by one is of little practical significance.

5. Read the function of any length: fread ()

The last way to read a file is to use the fread () function, which can read any length of bytes from the file. The basic structure of this function is as follows:
String fread (resource fp, int length );

When this function is used, it either fully reads the number of bytes specified by the length parameter, or reads the end of the file or ends the network packet.

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.