The correct way to read files in PHP (1) _php tutorial

Source: Internet
Author: User
Tags fread
Roger McCoy is a developer who uses a variety of programming languages, including C, Java, JavaScript, Perl, PHP, and Microsoft Visual Basic. He has five years of experience in developing PHP applications, but he may be more famous as a technician in the call center industry.

Let's figure out how many ways

One of the pleasures of dealing with modern programming languages such as PHP is that there are a number of options available. PHP can easily win Perl's motto "There's more than one ways to do it" (not the only way it can be done), especially in file processing. But in so many available options, which is the best tool to complete the job? Of course, the actual answer depends on the goal of parsing the file, so it's worth taking the time to explore all options.

The traditional method of fopen

The Fopen method is probably the most familiar to the previous C and C + + programmers, because if you have used these languages, they are more or less a tool that you have mastered for years. For any of these methods, open the file by using the standard method of fopen (the function used to read the data) and then use Fclose to close the file, 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 break them down. Perform the following steps effectively:

1. Open the file. $file _handle stores a reference to the file itself.
2. Check if you have reached the end of the file.
3. Continue reading the file until the end of the file is reached, and print each line while reading.
4, close the file.

Remembering these steps, I'll review each of the file functions used here.

fopen

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

$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 like a local file.

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

Feof

The feof command detects if you have read the end of the file and returns True or False. The loop in Listing 1 will continue until you reach the end of the file "MyFile". Note: If you read a URL and the socket times out because no longer has any data to read, feof will also return False.

Fclose

Jumping forward to the end of Listing 1, Fclose will implement the opposite function of fopen: It will close the connection to the file or URL. After you execute this function, you will no longer be able to read any information from a file or socket.

Fgets

Jumping back a few lines in Listing 1, you reached the core of 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 will print the entire file in fine detail.

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

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

Recall the end-of-string terminator in C, which sets the length to a number higher than the actual desired value. Thus, if 80 characters are required, the above example uses 81. You should get into the habit of adding this extra character whenever you use a row limit on this function.

Fread

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

At this point, you need to use fread. The Fread function is slightly different from the processing target of fgets: it tends to read information from a binary file (that is, a file that does not primarily contain human-readable text). Because the concept of "line" is not related to binary files (the logical data structure is not usually terminated by new lines), you must specify the number of bytes to read in.

$fh = fopen ("MyFile", "RB");
$data = Fread ($file _handle, 4096);

The above code reads 4,096 bytes (4 KB) of data. Note: No matter how many bytes are specified, Fread will not read more than 8,192 bytes (8 KB).

1

http://www.bkjia.com/PHPjc/446811.html www.bkjia.com true http://www.bkjia.com/PHPjc/446811.html techarticle Roger McCoy is a developer who uses a variety of programming languages, including C, Java, JavaScript, Perl, PHP, and Microsoft Visual Basic. He has five years of experience in developing PHP applications, but ...

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