Learn the right way to read files in PHP

Source: Internet
Author: User
Tags fread
Do read the file today to practice when encountered a problem, and then in Baidu to find relevant solutions. Found this article, I feel good, special reproduced over, for everyone's reference, I believe that we will have a different harvest.

The Fopen method may be the most familiar to previous C and C + + programmers, because if you have used these languages, they are more or less the tools that you have mastered for years. For either of these methods, open the file by using the standard method of fopen (the function to read the data), and then close the file using Fclose, as shown in Listing 1.

Listing 1: Opening and reading files with fgets

$file _handle = fopen ("MyFile", "R");

while (!feof ($file _handle)) {

$line = fgets ($file _handle);

Echo $line;

}

Fclose ($file _handle);

Although most programmers with years of programming experience are familiar with these functions, let me decompose these functions. Effectively perform the following steps:

1, open the file. $file _handle stores a reference to the file itself.

2, check whether you have reached the end of the file.

3, continue to read the file until the end of the file, the edge of the reading side of the print each line.

4, close the file.

With these steps in mind, I'll review each of the file functions used here.

fopen

The fopen function creates a connection to the file. The reason I say "create a connection" is because, in addition to opening the file, fopen can open a URL:

$fh = fopen ("HTTP://127.0.0.1/", "R");

This line of code creates a connection to the above page and allows you to start reading it as if you were reading a local file.

Note: the "R" used in fopen will indicate that the file is opened as read-only. Because writing data to a file is not covered in this article, I will not list all the other options. However, if you are reading from a binary file to obtain Cross-platform compatibility, you should change "R" to "RB". You'll see an example of this later.

Feof

The feof command detects whether you have read to the end of the file and returns True or False. The loop in Listing 1 continues until you reach the end of the file "MyFile". Note: If the URL is read and the socket times out because no more data can be read, feof also returns FALSE.

Fclose

Skip forward to the end of Listing 1, and fclose will implement the function opposite to fopen: it closes the connection to the file or URL. After you perform this function, you will no longer be able to read any information from a file or socket.

Fgets

If you jump back a few lines in Listing 1, you get to the core of the file processing: actually read the file. The Fgets function is the preferred weapon for handling the first example. It extracts a row of data from the file and returns it as a string. After that, you can print or otherwise process the data. The example in Listing 1 prints the entire file in fine detail.

If you decide to limit the size of the processed data blocks, you can add a parameter to the fgets to limit the maximum length of the line. For example, use the following code to limit the length of a line to 80 characters:

$string = fgets ($file _handle, 81);

Recall the Terminator at the end of the string in C to set the length to a number larger than the actual desired value. Thus, if 80 characters are required, the above example uses 81. You should develop the following habits: Add the extra character as long as the line limit is used for this function.

Fread

The Fgets function is available in more than one by one file read functions. It is a more commonly used function, because row-by-line parsing usually makes sense. In fact, several other functions can also provide similar functionality. However, you do not always need to parse line by row.

Then you need to use fread.

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.